Vector loops

Im not sure why vector d is only getting the "dog" string and not the "catd" string as well.

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
  /******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <string>
#include <vector> 

using namespace std;

int main()
{
    vector<string>x;
    vector<string>d;
    x.push_back("dog");
    x.push_back("catd");
    for(int i =0;i<x.size();i++){
        for(int j=0;j<x.size();j++){
        if(x[i][j]=='d'){
            d.push_back(x[i]);
        }
        }
    }
    for(int v=0;v<d.size();v++){
        cout<<d[v];
    }

    return 0;
}
closed account (E0p9LyTq)
Your vectors are not two dimensional, you can't access the elements using 2D notation.

To search for specified characters within a std::string you need to use the string find functions.

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
#include <iostream>
#include <string>
#include <vector> 

int main()
{
   std::vector<std::string>x;
   std::vector<std::string>d;
   
   x.push_back("dog");
   x.push_back("turtle");
   x.push_back("catd");
   x.push_back("fish");

   for (auto itr : x)
   {
      if (itr.find('d') != std::string::npos)
      {
         d.push_back(itr);
      }
   }

   for (auto itr : d)
   {
      std::cout << itr << ' ';
   }
   std::cout << '\n';
}

dog catd

I prefer using range-based for loops when dealing with C++ containers if I need to access every element. No mistakes from going out of bounds.
http://en.cppreference.com/w/cpp/language/range-for
> Im not sure why vector d is only getting the "dog" string and not the "catd" string as wel

There is a typo in the loop on line 22.

1
2
// for(int j=0;j<x.size();j++){ // line 22
for( int j=0 ; j < x[i].size() ; j++ ){ 


I suppose that you do realise that if in a string in vector x there are multiple occurrences of 'd',
it would get pushed back multiple times.
closed account (E0p9LyTq)
If you want to use standard for loops:

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
#include <iostream>
#include <string>
#include <vector> 

int main()
{
   std::vector<std::string> x;
   std::vector<std::string> d;
   
   x.push_back("dog");
   x.push_back("turtle");
   x.push_back("catd");
   x.push_back("fish");

   for (size_t loop = 0; loop < x.size(); loop++)
   {
      if (x[loop].find('d') != std::string::npos)
      {
         d.push_back(x[loop]);
      }
   }

   for (size_t loop = 0; loop < d.size(); loop++)
   {
      std::cout << d[loop] << ' ';
   }
   std::cout << '\n';
}
what is std::string::npos?
closed account (E0p9LyTq)
std::string::npos
http://en.cppreference.com/w/cpp/string/basic_string/npos

If no matches are found, the string find functions return std::string::npos.
Last edited on
Topic archived. No new replies allowed.