0

Having this:

#include <iostream> #include <vector> #include <string> using namespace std; struct Info{ string word; unsigned int i; }; vector<Info> unique(vector<Info> &vec){ vector<Info> ret; size_t len = vec.size(); for(int i=0; i<len; i++){ int j = 0; for(; j<len; j++){ if(vec[i].word == vec[j].word){ vec[i].i++; break; } } if(j>=len){ Info info{vec[i].word, 0}; ret.push_back(info); } } return ret; } int main(){ vector<Info> words, origs; string tmp; while(cin >> tmp){ Info info{tmp, 0}; words.push_back(info); } origs=unique(words); cout << "number of elements of vector: " << words.size() << ", of which are unique: "; for(int i=0; i<origs.size(); i++){ cout << origs[i].word << " "; } } 

Should gives unique elements of vector, but if I run it: foo.cpp:

a b c b c number of elements of vector: 5, of which are unique: 

No unique, values, that should be obtained by origs=unique(words);, and the result from above should be a b c. I am not sure, if break breaks only from the if block, or also from the inner for loop (which is intented). Can someone help?

1
  • 4
    break breaks the innermost loop, but the rest of your question is word salad; I can't tell what you're asking (and it seems to have nothing to do with the title). Commented Jul 7, 2020 at 16:05

3 Answers 3

1

break is used to break from loop not from if conditions so yes break breaks from the inner loop. It will be an easier approach to check for the value in ret vector if it exists don't push it.

vector<Info> unique(vector<Info> &vec){ vector<Info> ret; size_t len = vec.size(); for(int i=0; i<len; i++){ boolean exists = false; for(int j = 0; j < ret.size(); j++){ if(vec[i].word == ret[j].word){ exists = true; break; } } if(!exists){ Info info{vec[i].word, 0}; ret.push_back(info); } } return ret; } 
Sign up to request clarification or add additional context in comments.

2 Comments

This works, but why did not worked by original version? The point was that if the inner loop did not finish (because of break), then the j variable was smaller then len (j<len), and thus the there was duplicate (otherwise no break and create new Info). This is fairly easy, so where was the problem? The break as you suggest breaks from innermost loop for(int j...), which still makes sense for my previous version (the condition is after the inner most loop), so again, where was bug from the previous version????
The problem is that you are comparing the element to itself, then no matter what it will always break. look at the example a b c b c when you start with i = 0 and j = 0 you are comparing vec[0] == vec[0] which will be true. and you are doing the same for all other elements that's why the ret vector was empty
0

break will terminate the innermost loop that it is located in, which in this case is your first nested for loop.

if(j>=len){ Info info{vec[i].word, 0}; ret.push_back(info); } 

This block never executes which is probably why your origs vector is always empty.

Comments

0

Both i and j index over the entire vector.

Thus, you compare the first element to itself on the first iteration, the second element to itself on the second iteration, and so on until you have compared every element to itself and found all of them equal to something.
Because of this, nothing is ever added to ret.

I think the simplest solution is to compare the elements of vec to the elements of ret instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.