Extract char elements from vector string

what is wrong in this : (really helpful if someone explains concept thanks : ) )

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

using namespace std;

int main()
{

vector<string> num={"sad sad","sdsadsadsadas","asdsasa"};

int count=0;

for(vector<string>::iterator it=num.begin(); it!=num.end(); ++it)
{

{
for(int x=0; x<*it.size(); ++x)

cout<<*it[x];

}

}

return 0;
}
order of operations.
try (*it).size() and (*it)[x]

its trying to do the .size() before it digs into the element etc
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <string>

int main()
{
   std::vector<std::string> num = { "sad sad","sdsadsadsadas","asdsasa" };

   for (std::vector<std::string>::iterator it = num.begin(); it != num.end(); ++it)
   {
      for (size_t x = 0; x < it->size(); ++x)
      {
         std::cout << (*it)[x];
      }
      std::cout << '\n';
   }
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <string>

int main()
{
   std::vector<std::string> num = { "sad sad","sdsadsadsadas","asdsasa" };

   for (auto vec_itr = num.cbegin(); vec_itr != num.cend(); vec_itr++)
   {
      for (auto str_itr = vec_itr->cbegin(); str_itr != vec_itr->cend(); str_itr++)
      {
         std::cout << *str_itr;
      }
      std::cout << '\n';
   }
}


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <string>

int main()
{
   std::vector<std::string> num = { "sad sad","sdsadsadsadas","asdsasa" };

   for (const auto& vec_itr : num)
   {
      for (const auto& str_itr : vec_itr)
      {
         std::cout << str_itr;
      }
      std::cout << '\n';
   }
}
THANKS GUYS
Topic archived. No new replies allowed.