Reputation: 27
I have 5 int vectors v1 to v5. I want to count the index of the same vectors separately. If a different vector appears in the next vector, it outputs the index of the previous same vectors and starts counting for the new vector. Any help would be appreciated.
std::vector<int>v1={1, 2, 3};
std::vector<int>v2={1, 2, 3};
std::vector<int>v3={1, 2, 3, 4};
std::vector<int>v4={1, 2, 3, 4};
std::vector<int>v5={1, 2, 3};
The output should be: vector value = vector of index
{1,2,3} = {0,1};
{1,2,3,4} = {2,3};
{1,2,3} = {4};
Upvotes: 0
Views: 374
Reputation: 8218
You want to just search the collection of vectors and find in which positions are the elements that match your criterion? Then maybe like this:
std::vector<int> v1={1, 2, 3};
std::vector<int> v2={1, 2, 3};
std::vector<int> v3={1, 2, 3, 4};
std::vector<int> v4={1, 2, 3, 4};
std::vector<int> v5={1, 2, 3};
std::vector<std::vector<int>> collection({v1, v2, v3, v4, v5});
auto found = std::find(collection.begin(), collection.end(), v1);
while (found != collection.end())
{
std::cout << "found at idx = " << std::distance(collection.begin(), found) << std::endl;
// begin next search after previously found element
found = std::find(++found, collection.end(), v1);
}
Can be also with custom predicate if you prefer.
Upvotes: 1