anyone can solve this old problem?

http://www.cplusplus.com/forum/beginner/26873/
I came across the same problem with the above link, it seems that that problem hasn't been solved yet. Anyone can help me?
It seems straight forward enough. What part of the problem are you having difficulty with?
question:write a program to compare whether a vector<int> contains the same elements as a list<int>.
my codes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <list>
using namespace std;

int main()
{
	vector<int> ivec;
	list<int> ilst;
	int ival;
	cout << "Enter several numbers for vector<int>:" << endl;
	while (cin >> ival)
	{
		ivec.push_back(ival);
	}
	cin.clear();
	cin.sync();	
	cout << "Enter several numbers for list<int>:" << endl;
	while (cin >> ival)
	{
		ilst.push_back(ival);
	}
	
	if (ivec.size() != ilst.size())
	{
		cout << "ivec != ilst" << endl;
		return -1;	
	}
	
	vector<int>::iterator vit = ivec.begin();
	list<int>::iterator lit = ilst.begin();
	
	while (vit != ivec.end())
	{
		if (*vit != *lit)
		{
			cout << "ivec != ilst" << endl;
			return -1;
		}
		else
		{
			++vit;
			++lit;
		}
	}
	cout << "ivec == ilst" << endl;
	return 0;
}

on mac ox x lion, I use the gcc compiler and the output:

Enter several numbers for vector<int>:
1
2
3
// I entered control + D here
Enter several numbers for list<int>:
ivec != ilst

I have no opportunity to input list<int> container!
but this program runs well on windows using MinGW compiler.
how to solve this problem? thx in advance!
If you can put all the numbers on the same line, you can read the whole line and process that. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::string line;

    std::cout << "Please enter numbers for vector on the same line" << std::endl;
    std::getline(std::cin, line);
    {
        std::istringstream is(line);
        int value;
        while (is >> value)
            // append value to the vector
            ;
    }

    std::cout << "Please enter numbers for list on the same line" << std::endl;
    std::getline(std::cin, line);
    {
        std::istringstream is(line);
        int value;
        while (is >> value)
            // append value to the list
            ;
    }

    return 0;
}

if you can put all the numbers on the same line, you can read the whole line and process that. For example:
#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string line;

std::cout << "Please enter numbers for vector on the same line" << std::endl;
std::getline(std::cin, line);
{
std::istringstream is(line);
int value;
while (is >> value)
// append value to the vector
;
}

std::cout << "Please enter numbers for list on the same line" << std::endl;
std::getline(std::cin, line);
{
std::istringstream is(line);
int value;
while (is >> value)
// append value to the list
;
}

return 0;
}

I know its an alternative method, but I still want to know what the problem with my program?
Last edited on
If you end the stream with control + D I'm not sure if you can enter more input later. Note that cin.sync(); doesn't do anything with gcc so you could try to use something else like cin.ignore(numeric_limits<streamsize>::max(), '\n');
Actually, even I use
cin.ignore(numeric_limits<streamsize>::max(), '\n');
instead of
cin.sync();
the problem is still the same...
It's driving me crazy
Like Peter said, you've ended the input stream. The system has no reason to expect further input to appear. Imagine you were redirecting a file into the standard input of the program -- there's no way to redirect *two* files.

On the other hand, Mac appears to be alone in this: I just ran your program on Linux (gcc), Solaris (CC), AIX (xlc), and HP-UX (acc), and they all accept further input after cin.clear(). I don't have a Mac system, but give cin.sync_with_stdio(false); a try.

(and don't use cin.sync(), of course)
Last edited on
> Mac appears to be alone in this

Mac is FreeBSD 5.5; I guess all the BSDs would behave in the same way.
I just tested it.

OS X (Lion) and FreeBSD (7.2) have the same behaviour, cin cannot be cleared.

It's fine on Linux.
In the BSDs, fpurge(stdin) ; is used to flush stdin (or any input stream).
http://www.freebsd.org/cgi/man.cgi?query=fpurge&apropos=0&sektion=0&manpath=FreeBSD+9.0-RELEASE&arch=default&format=html

Apparently, the GNU libstdc++ doesn't use it.
Topic archived. No new replies allowed.