By Vector loop skips odd numbers

I want to know why my remove function isn't removing all of its elements when called and isn't removing odd vector elements? If i input the words one, two, three, four then hit control z the two and the four will still remain in the vector and wont be printed out into my list.

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
49
#include <iostream>
#include <vector>
#include <string>
using std::string; using std::vector;
int occured(const vector<string> csvec, const string s);
void remove(vector<string>& csvec, const string s);
int main()
{
	vector<string> svec;
	string s;
	int count = 0;
	while (std::cin >> s) {
		svec.push_back(s);
		count++;
	}
	std::cout << std::endl;
	std::cin.clear();

	for (auto i = 0; i < svec.size(); i++) {
		std::cout << occured(svec, svec[i]) << "   " << svec[i] << std::endl;
		remove(svec, svec[i]);
	}

	std::cout << "You typed in " << count << " words" << std::endl;
	if (svec.empty())
		std::cout << "The Vector is empty" << std::endl;
	else {
		for(auto i = 0; i < svec.size(); ++i) 
			std::cout << svec[i] << " ";
	}
		
	std::cin.get();
	return 0;
}
int occured(const vector<string> csvec, const string s) {
	int count = 0;
	for (auto x = 0; x < csvec.size(); ++x) {
		if (csvec[x] == s)
			count++;
	}
	return count;
}
void remove(vector<string>& csvec, const string s) {
	for (auto i = 0; i < csvec.size(); i++) {
		if (csvec[i] == s) {
			csvec.erase(csvec.begin() + i);
		}
	}
}
Last edited on
Trace your code carefully.

When the loop on line 19 calls the remove function, it erases the element at position i. All well and good. Problem is that the next element is now at position i, but the for-loop at line 19 goes on to the next index (i+1), and calls remove on that element, skipping the ith element. Hence, the vector removes all even positions and leaves the odd ones.
Last edited on
Ah I see what you mean...hhmmm yh i'm really stumped on how to fix it i tryed setting i to element 0 on each time through it displayed correctly on the screen but then the problem was i go a vector error message.
Last edited on
So just so Im clear, you want to remove all the elements in the vector?

What I don't understand is why you even have the else clause on line 27 if the for-loop before it was intended to empty the vector.
Perhaps this is what you want:

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
#include <iostream>
#include <vector>
#include <string>
using std::string; using std::vector;
int occured(const vector<string> csvec, const string s);
int main()
{
	vector<string> svec;
	string s;
	int count = 0;
	while (std::cin >> s) {
	    if(s == "q") break; // used to break out of while loop on cpp.sh
		svec.push_back(s);
		count++;
	}
	std::cout << std::endl;
	std::cin.clear();

	for (auto i = 0; i < svec.size(); i++) {
		std::cout << occured(svec, svec[i]) << "   " << svec[i] << std::endl;
	}
	
	svec.clear();

	std::cout << "You typed in " << count << " words" << std::endl;
	std::cout << "The Vector is empty" << std::endl;

		
	std::cin.get();
	return 0;
}
int occured(const vector<string> csvec, const string s) {
	int count = 0;
	for (auto x = 0; x < csvec.size(); ++x) {
		if (csvec[x] == s)
			count++;
	}
	return count;
}
Last edited on
http://en.cppreference.com/w/cpp/container/vector/clear


Lol, how embarrassing. Code updated.
Yh i put that else statement to see what was still inside the vector when it should be empty.
Last edited on
Finally I finished it, i realised now that i needed another vector in order to check that the word hasn't already been used thanks for the help though.
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 <string>
using std::string; using std::vector;
int occured(const vector<string> csvec, const string s);
bool check(string s, const vector<string>& csvec);
int main()
{
	vector<string> svec;
	vector<string> svec2;
	string s;
	int count = 0;
	while (std::cin >> s) {
	if (s == "q") break;
		svec.push_back(s);
		count++;
	}
	std::cout << std::endl;
	std::cin.clear();

	for (auto i = 0; i < svec.size(); i++) {
		if (check(svec[i], svec2)) {
			std::cout << occured(svec, svec[i]) << "   " << svec[i] << std::endl;
			svec2.push_back(svec[i]);

		}
	}

	svec.clear();
	std::cout << "You typed in " << count << " words" << std::endl;
	std::cin.get();
	return 0;
}
int occured(const vector<string> csvec, const string s) {
	int count = 0;
	for (auto x = 0; x < csvec.size(); ++x) {
		if (csvec[x] == s)
			count++;
	}
	return count;
}
bool check(string s, const vector<string>& csvec) {
	for (auto i = csvec.begin(); i != csvec.end(); ++i) {
		if (s == *i)
			return false;
	}
	return true;
}

The task I was given was to write a program that reads input from a stream and stores them in a vector. Use that function to both write the count for the number of words in the vector and to count how many times each word is used, I didnt like that the old program wrote multiple of the same word.
Last edited on
Topic archived. No new replies allowed.