-2

Let's say I have a function int pozvec(vector<string> vect) which takes as parameter a pre-filled vector of strings.

I want to know at which position i there is the character "p", specifically "p". All of the strings are single-character, but the vector is declared as containing strings. I want to save the index at which "p" is found in a variable x;

for(int i=0; i<vect.size(); i++)
    if(vect[i] == "p")
        x=i;

does not seem to work. Nor does strcmp, at all. The problem is more complex, but the part that doesn't work is this one.

Thank you in advance for your help!

AndrewFNAF
  • 11
  • 4
  • 2
    Use [`std::find`](https://en.cppreference.com/w/cpp/algorithm/find). – Evg Mar 02 '21 at 08:06
  • For me, your code works just fine as you can see [here](https://godbolt.org/z/7znPEr). You get the position of the last occurence of ```"p"``` in your vector. – StefanKssmr Mar 02 '21 at 08:10

2 Answers2

0

By that function, your storing the index of the last occurrence of the string your looking for. This is because your not exiting from the loop once the first occurrence was found.

To exit from a loop conditionally, use the break statement.

for(int i = 0; i< vect.size(); i++)
{
    if(vect[i] == "p")
    {
        x=i;
        break;
    }
}

Now it will exit right after the first occurrence was found.

You also have another option in the form of std::find.

int index = std::find(vect.begin(), vect.end(), "P") - vect.begin()
D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • Hello! Thank you. I forgot to mention that I know for sure that the string appears exactly once. – AndrewFNAF Mar 04 '21 at 07:12
  • @AndrewFNAF If that's the case, this will work just fine. – D-RAJ Mar 04 '21 at 14:47
  • But what if I know the position where it COULD BE exactly, I just want to check whether or not it's there? Say the vector is of size n and I want to check SPECIFICALLY if "p" is at position [n*(n-1)], such that I don't have to go through the whole vector. Can I use if() in that case? – AndrewFNAF Mar 04 '21 at 15:31
  • @AndrewFNAF Yeah just use `if (vect[n * (n - 1)] == "p") { ... }` if you want to check one specific location/ index. – D-RAJ Mar 04 '21 at 16:05
  • Except that does not work. That's...the reason I asked. – AndrewFNAF Mar 23 '21 at 09:11
0

I don't know what you really want to do, but this worked perfectly fine for me:

for (size_t i {}; i < vec.size(); ++i) {
   if (vec.at(i) == "p") {
       x = i;
       break; // I don't know wether you want to break here or not
   } else {
       continue;
   }
}

Or if you all strings include just one character this should also work:

for (size_t i {}; i < vec.size(); ++i) {
   if (vec.at(i).at(0) == 'p') {
       x = i;
       break; // I don't know wether you want to break here or not
   } else {
       continue;
   }
}
Creighton
  • 1
  • 1
  • You don't need the ```else{ continue;}``` block in you code. – StefanKssmr Mar 02 '21 at 08:11
  • Yes, that part is optional, but it may be useful in some cases. – Creighton Mar 02 '21 at 08:13
  • What does the ”at” thing do? I am more experienced with arrays. Thank you! – AndrewFNAF Mar 04 '21 at 07:11
  • The .at() method is only available for STL containers. It is almost the same as [], so I could replace vec.at(i) with vec[i]. However, the .at() method does bounds checking, and [] does not. For instance, if I have a vector of 5 elements and I call vec.at(7), it will throw an exception, but if I say vec[i], it does no bounds checking and the result will be really strange. So you can control you flow calling .at(), but cannot with [], so you should use .at() like every time. – Creighton Mar 04 '21 at 18:51