finding string in vector.

What i need is to check if a string that the user inputs exists in the vector then save the location to be used to refer to a different vector.
If any one has any ideas that would be great.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  int playerStats(vector<int>&PLAYER_STATS,vector<string>stats){
    if (PLAYER_STATS[0]>=100){
        cout<<"\nCongratulations you have levelled up!\n You have 7 points to spend, so choose wisely.";
        vector<int>::const_iterator inIter;
        vector<string>::const_iterator stIter;
        stIter=stats.begin();
        for(inIter=PLAYER_STATS.begin();inIter!=PLAYER_STATS.end();++inIter){
            cout<<*stIter++<<"\t";
            cout<<*inIter;
            }
            cout<<"Enter any of the stats above to add one point.";
        for(int point=1;point!=8;++point){
            cout<<"What would you like to add point number "<<point<<" to? ";
            string add;
            cin>>add;
            for (int i=0;i<add.length();i++)
                add[i]=tolower(add[i]);
             while(){/*check word against vector "stats" if it isn't there do this:*/
                cout<<"You have entered a invalid command. Please re-enter: ";
                cin>>add;
            }
            //get position of "add" in vector "stats"
            //add 1 to same location in "PLAYER_STATS"
}}

If this has already been asked (or something similar) a link will do just fine.
auto match = std::find( begin(stats), end(stats), keyword );
if ( end(stats) != match ) auto index = std::distance( begin(stats), match );
Thank you, could you explain where to put what? also a brief explanation would be great so i know how to use it in the future.
This has two examples.

The first uses a search predicate to call an externally defined functor. You should be able to step thru this in the debugger if it isn't clear.

The last statement does the same thing, but in two lines using C++11 features, an improved for loop and a lambda.
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
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

struct PartialMatch
{
        std::string s;

        PartialMatch(const std::string& str) : s(str) {}

        bool operator()(const std::string& in)
        {
                size_t pos = in.find(s);
                return pos != std::string::npos;
        }
};

typedef std::vector<std::string> strvec_t;

int main()
{
    strvec_t v;
    v.push_back("apple");
    v.push_back("mango");
    v.push_back("tangerine");

    strvec_t s;
    s.push_back("app");
    s.push_back("aplet");

    for (strvec_t::const_iterator p = s.begin(); p != s.end(); ++p)
        std::cout << "Match(\"" << *p << "\") = " << (std::find_if(v.begin(), v.end(), PartialMatch(*p)) != v.end()) << std::endl;

    for (const auto& entry : s)
        std::cout << "Match(\"" << entry << "\") = " << (std::find_if(v.begin(), v.end(), [entry](const std::string& in){return in.find(entry)!= std::string::npos;}) != v.end()) << std::endl;
}
Last edited on
Nice one, thank you.
I know i have marked this as answered but I had it working fine, but i must have changed something cause it is going strait the the else clause even if the input is valid and I can't figure out why. Here is the code:
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
int playerStats(vector<int>&PLAYER_STATS,vector<string>stats){
    if (PLAYER_STATS[0]>=100){
        PLAYER_STATS[0]-=100;
        cout<<"\nCongratulations you have levelled up!\n You have 7 points to spend, so choose wisely.\n\n";
        vector<int>::iterator inIter;
        vector<string>::const_iterator stIter;
        stIter=stats.begin();
        for(inIter=PLAYER_STATS.begin();inIter!=PLAYER_STATS.end();++inIter){
            cout<<*stIter++<<"\t";
            cout<<*inIter<<endl;
            }
        cout<<"Enter any of the stats above to add one point.\n";
        for(int point=1;point!=8;++point)
        {
            cout<<"What would you like to add point number "<<point<<" to? ";
            string add;
            cin>>add;
            for (int i=0;i<add.length();i++)
                add[i]=tolower(add[i]);
            bool invalid=true;
            while(invalid)
            {
                auto match = find(begin(stats)+2,end(stats),add);
                if ( end(stats) != match )
                {
                    auto index = distance( begin(stats), match );
                    cout<<index;
                    invalid=false;
                    PLAYER_STATS[index]++;
                }
                    else
                    {
                        cout<<"You have entered a invalid command. Please re-enter: ";
                        cin>>add;
                        invalid=true;
                    }
            }
        }
    }

I believe line 24 is returning as false so it goes to else.
You use:
 
auto match = find(begin(stats)+2,end(stats),add);

where stats is:
 
vector<string>stats


What happens if stats.size() < 2?

Also, you should pass stats by const ref to avoid copying that vector.
stats is set to 7 (i defined stats in my main function)

And thanks i will do.
Last edited on
"I must have changed something"
Start reading about version control systems. Git is nice.

You do lowercase only the first attempt.

You have two vectors that should be of same size. Consider using only one vector that contains std::pair<string,int> objects, or replace vector with std::map<string,int>. Map has find().
nice i have heard a little about maps but never used them, also didn't know there was a pair.
Topic archived. No new replies allowed.