| |

VerySource

 Forgot password?
 Register
Search
View: 27|Reply: 11

happy New Year to all! Can you help me modify the small topic?

[Copy link]

3

Threads

11

Posts

8

Credits

Newbie

Rank: 1

Credits
8

 China

Post time: 2020-1-24 06:40:01
| Show all posts |Read mode
Set an integer to implement simple operations on integers
Written in vector
class IntSet
{
private:
    vector <int> Ints;
int element [100];
int n, seeknumber;
public:
IntSet ()
{
cout << "Enter the following integer:";
cin >> n;
for (long i = 0; i <n; i ++)
{
cin >> element [i];
Ints.push_back (element [i]);
};
cout << endl << Ints [0];
};
Empty () {Ints.clear ();};
Isempty () {if (Ints.empty ()) cout << "The collection is empty!" << endl;};
bool Ismemberof ()
{
cout << "To determine if this number is inside the collection, please enter it:";
cin >> seeknumber;
for (long j = 0; j <n; j ++)
Ranch
if (Ints [j] == seeknumber) return true;
Ranch
};
Add () {};
Sub () {};
Isequal () {};
Intersection () {};
Merge () {};
Print () {};
};
Is there anything wrong, please help me modify it? Thank you all here!
Reply

Use magic Report

0

Threads

6

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-2-9 21:15:02
| Show all posts
for (long i = 0; i <n; i ++)
{
cin >> element [i];
Ints.push_back (element [i]);
};
-------------------------------------------------- -
In fact, the array element is superfluous.


bool Ismemberof ()
{
cout << "To determine if this number is inside the collection, please enter this number:";
cin >> seeknumber;
for (long j = 0; j <n; j ++)
Ranch
if (Ints [j] == seeknumber) return true;
Ranch
};
-------------------------------------------------- --------------------
This lookup can be implemented using find (),
Or use vector <int> :: iterator to locate the value of seeknumber!


The ";" after the class is required, but the ";" after the function is not needed, and it is not wrong to add it, but it is not part of the function, but is regarded as a blank line by the compiler.

If you haven't forgotten the corresponding header file, there should be no problem.
Reply

Use magic Report

0

Threads

6

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-2-10 12:00:01
| Show all posts
cout << endl << Ints [0];
--------------------------------
Should it be changed to
cout << endl << * Ints.Begin ();

??
Reply

Use magic Report

0

Threads

38

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-3-3 16:00:01
| Show all posts
Although the usage is not very good, the compilation should pass. Likeeyesloveyou, element is useless here, and the input can be changed to:
 for (long i = 0; i <n; i ++)
{
         long temp;
cin >> temp;
Ints.push_back (temp);
};
To use find (), you need to include the header <algorithm>, as follows:
#include <algorithm>
usage:
vector <int> :: iter = find (Ints.begin (), Ints.end (), seeknumber);
if (iter! = Ints.end ())
return true;
else
return false;



In addition:
cout << endl << Ints [0];
no problem
Reply

Use magic Report

0

Threads

38

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-3-3 19:45:01
| Show all posts
Temp is a local variable, because containers such as vector all use value passing, so there will be no problem
Reply

Use magic Report

3

Threads

11

Posts

8

Credits

Newbie

Rank: 1

Credits
8

 China

 Author| Post time: 2020-3-9 14:00:01
| Show all posts
Thank you guys!
So grateful
Reply

Use magic Report

3

Threads

11

Posts

8

Credits

Newbie

Rank: 1

Credits
8

 China

 Author| Post time: 2020-3-10 12:15:01
| Show all posts
What else should be added on this subject? ?
I've changed it, but I feel that there should be something added, who knows
Reply

Use magic Report

0

Threads

68

Posts

25

Credits

Newbie

Rank: 1

Credits
25

 China

Post time: 2020-3-18 17:15:01
| Show all posts
class IntSet
{
private:
    vector <int> Ints;
int n, seeknumber, element;
public:
IntSet ()
{
cout << "Enter the following integer:";
cin >> n;
for (long i = 0; i <n; i ++)
{
cin >> element;
Ints.push_back (element);
};
// cout << endl << Ints [0];
}
void Empty () {Ints.clear ();}
bool Isempty ()
{
      if (Ints.empty ()) {cout << "The collection is empty!" << endl; return true;}
      else return false;
    }
bool Ismemberof ()
{
cout << "To determine if this number is inside the collection, please enter this number:";
cin >> seeknumber;
for (long j = 0; j <n; j ++)
if (Ints [j] == seeknumber) return true;
return false;
Ranch
};
Ranch
bool Isequal () {};
IntSet&Add () {};
IntSet&Sub () {};
IntSet&Intersection () {};
IntSet&Merge () {};
void Print () {};
};

// test
int main ()
{
    IntSet set1;
    set1.Isempty ();
    // .... other
    system ("pause");
    return 0;
}
Reply

Use magic Report

0

Threads

68

Posts

25

Credits

Newbie

Rank: 1

Credits
25

 China

Post time: 2020-3-23 08:00:02
| Show all posts
bool Isequal () {};
IntSet&Add () {};
IntSet&Sub () {};
IntSet&Intersection () {};
IntSet&Merge () {};
void Print () {};

These needs continue to be achieved ~~
Reply

Use magic Report

3

Threads

11

Posts

8

Credits

Newbie

Rank: 1

Credits
8

 China

 Author| Post time: 2020-4-8 15:30:01
| Show all posts
How else? I am not very clear!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list