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.
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.
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.
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.
#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)
returnfalse;
}
returntrue;
}
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.