Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get position of a certain element in strings vector, to use it as an index in ints vector?

Tags:

c++

I am trying to get the index of an element in a vector of strings, to use it as an index in another vector of int type, is this possible ?

Example:

vector <string> Names; vector <int> Numbers;   ...  // condition to check whether the name exists or not if((find(Names.begin(), Names.end(), old_name_)) != Names.end())       {   // if yes         cout <<"Enter the new name."<< endl;         cin >> name;         replace(Names.begin(), Names.end(), old_name_, name);     } 

Now I want to get the position of old_name in the Names vector, to use it in accessing certain element in Numbers vector. So that I can say:

Numbers[position] = 3 ; // or whatever value assigned here. 

I tried using:

vector <string> :: const_iterator pos; pos = (find(Names.begin(), Names.end(), old_name_)) Numbers[pos] = 3; 

but obviously this doesn't work since pos is of type string !

like image 910
Nour Avatar asked Feb 26 '13 21:02

Nour


People also ask

How do you get the position of an element in a vector?

find(): Used to find the position of element in the vector. Subtract from the iterator returned from the find function, the base iterator of the vector . Finally return the index returned by the subtraction.

What method can be used to determine the number of elements in a vector?

Use the Vector. size method. It will tell you the number of elements in the vector.

How do you find the position of the maximum element in a vector?

To find the largest or smallest element stored in a vector, you can use the methods std::max_element and std::min_element , respectively. These methods are defined in <algorithm> header. If several elements are equivalent to the greatest (smallest) element, the methods return the iterator to the first such element.


2 Answers

To get a position of an element in a vector knowing an iterator pointing to the element, simply subtract v.begin() from the iterator:

ptrdiff_t pos = find(Names.begin(), Names.end(), old_name_) - Names.begin(); 

Now you need to check pos against Names.size() to see if it is out of bounds or not:

if(pos >= Names.size()) {     //old_name_ not found } 

vector iterators behave in ways similar to array pointers; most of what you know about pointer arithmetic can be applied to vector iterators as well.

Starting with C++11 you can use std::distance in place of subtraction for both iterators and pointers:

ptrdiff_t pos = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_)); 
like image 69
Sergey Kalinichenko Avatar answered Sep 23 '22 12:09

Sergey Kalinichenko


If you want an index, you can use std::find in combination with std::distance.

auto it = std::find(Names.begin(), Names.end(), old_name_); if (it == Names.end()) {   // name not in vector } else {   auto index = std::distance(Names.begin(), it); } 
like image 40
juanchopanza Avatar answered Sep 24 '22 12:09

juanchopanza