How to find the element number of a vector<string>?

Hello,

Imagine I have a vector as such...

 
  vector<string> vec = {"alpha", "beta", "gamma", "delta"};


and I wanted to output "gamma", I would use;

 
  cout << vec[2];


which would return "gamma".

So; input (2) -> output ("Gamma")

Now I am looking for the exact opposite of this.
Using the exact same vector above, is it possible to return the element/ index value of 2 if I use "gamma" as an input of some other function (it obviously won't work if I type vec["gamma"], but is there another function?)

I.e; input ("Gamma") -> output (2).




Last edited on
Try either (general vector type)
1
2
3
4
5
6
7
8
#include<vector>
using namespace std;

template <class T> int getIndex( vector<T> v, T value )
{
   for ( int i = 0; i < v.size(); i++ ) if ( v[i] == value ) return i;
   return -1;
}



or using the "find" algorithm:
1
2
3
4
5
6
7
8
9
#include<vector>
#include<algorithm>
using namespace std;

template <class T> int getAnotherIndex( vector<T> v, T value )
{
   int i = find( v.begin(), v.end(), value ) - v.begin();
   return ( i == v.size() ) ? -1 : i;
}



or character arrays (not quite the same as strings)
1
2
3
4
5
6
7
8
9
#include<vector>
#include<string>
using namespace std;

int getStringIndex( vector<string> v, const char *value )
{
   for ( int i = 0; i < v.size(); i++ ) if ( v[i] == value ) return i;
   return -1;
}




Some code to test them:
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
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

template <class T> int getIndex( vector<T> v, T value )
{
   for ( int i = 0; i < v.size(); i++ ) if ( v[i] == value ) return i;
   return -1;
}


template <class T> int getAnotherIndex( vector<T> v, T value )
{
   int i = find( v.begin(), v.end(), value ) - v.begin();
   return ( i == v.size() ) ? -1 : i;
}


int getStringIndex( vector<string> v, const char *value )
{
   for ( int i = 0; i < v.size(); i++ ) if ( v[i] == value ) return i;
   return -1;
}




// Testing ...

int main()
{
   // strings
   vector <string> vec;
   vec.push_back( "alpha" );
   vec.push_back( "beta"  );
   vec.push_back( "gamma" );
   vec.push_back( "delta" );
   cout << "gamma --> " << getIndex( vec, string( "gamma" ) ) << endl;         // should produce 2
   cout << "Gamma --> " << getIndex( vec, string( "Gamma" ) ) << endl;         // should produce -1 (not found)
   cout << "gamma --> " << getAnotherIndex( vec, string( "gamma" ) ) << endl;  // should produce 2
   cout << "Gamma --> " << getAnotherIndex( vec, string( "Gamma" ) ) << endl;  // should produce -1 (not found)
   // character arrays
   cout << "gamma --> " << getStringIndex( vec, "gamma" ) << endl;             // should produce 2
   cout << "Gamma --> " << getStringIndex( vec, "Gamma" ) << endl;             // should produce -1 (not found)


   // integers
   vector <int> veci;
   veci.push_back( 10 );
   veci.push_back( 20 );
   veci.push_back( 30 );
   veci.push_back( 40 );
   cout << "40 --> " << getIndex( veci, 40 ) << endl;         // should produce 3
   cout << "10 --> " << getIndex( veci, 10 ) << endl;         // should produce 0
   cout << "0  --> " << getIndex( veci,  0 ) << endl;         // should produce -1 (not found)
}
Last edited on
Hi thanks,
I found a simple way of doing it already. I think it's quite similar to your first method actually. :)

Thanks anyway!
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <vector>

enum class GreekAlphabet : std::size_t { alpha, beta, gamma, delta };

int main()
{
    std::vector<std::string> vec = {"alpha", "beta", "gamma", "delta"};
    
    std::cout << vec[static_cast<std::size_t>( GreekAlphabet::gamma )] << "\n";
}


gamma

Topic archived. No new replies allowed.