Passing an array using for_each loop

Pages: 12
i think i will really have to create two for_each loops.

i just thought that by appending new elements to an existing vector, i will be able to print original vector elements plus appended elements concurrently

this will be the final simple code sample:

1
2
3
4
5
6
7
8
std::cout << "Appending and then printing std::vector using for_each loop:" << std::endl; 
  std::vector<std::string> pokemon_moon{};
  std::vector<std::string> pokemon_moon_starter {"Rowlet", "Litten", "Popplio"};

  for_each(pokemon_moon_starter.begin(), pokemon_moon_starter.end(), [&](const auto i){
    pokemon_moon.push_back(i);
    std::cout << i << std::endl;
  });


I'll probably provide another sample code that includes some kind of algo.

thanks for the help, people :D
Ouch! onlinegdb appears to be using an out-dated version of GCC, 7.4.0.
RECAP just bc i think we are going to different tangents

I get that I can append an element to an empty vector and immediately print the recently added vector.

the issue was i thought it's possible to add new elements to an existing vector that is NOT empty and print all elements (new and original) concurrently.

as you see in the code i provided, i std::push_back() Pikachu, Charmander, and Squirtle to an empty std::vector called pokemon.

i then attempted to std::insert() an array of strings into pokemon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  std::vector<std::string> pokemon;

  pokemon.push_back("Pikachu"); //adds "Pikachu" to the vector
  pokemon.push_back("Charmander"); //adds "Charmander" to the vector
  pokemon.push_back("Squirtle"); //adds "Squirtle" to the vector
  
  std::cout << "Traditional std::vector() printing:" << std::endl;
  std::cout << pokemon[0] << std::endl; //prints "Pikachu"
  std::cout << pokemon[1] << std::endl; //prints "Charmander"
  std::cout << pokemon[2] << std::endl; //prints "Squirtle"
  std::cout << std::endl;
  
  
  //HERE IS WHERE I NEED HELP
  std::string pokemon_to_add[3] = {"Butterfree", "Onyx", "Arbok"};
  pokemon.insert(pokemon.end(), pokemon_to_add, pokemon_to_add + 3);
  //I reckon it would be something like:
  //for_each(pokemon.begin(), pokemon.end(), [](const auto &i){push_back(i);}); 


markyrocks then suggested that it will be much more legible if i were to append a vector to another opposed to an array of strings to a vector and also so that i won't have to deal with sizes. this is their example:

1
2
3
4
  vector<string> st{};
  vector<string> a{ "abc" , "123", "easy" };
	
  for_each(a.begin(), a.end(), [&st](const auto &i) {st.push_back(i); cout << i; });


and so i modeled my code after theirs:

1
2
3
4
5
6
7
8
  std::vector<std::string> pokemon{};

  std::vector<std::string> pokemon_to_add {"Butterfree", "Onyx", "Arbok"};

  for_each(pokemon_to_add.begin(), pokemon_to_add.end(), [&](const auto &i) {
    pokemon.push_back(i);
    std::cout << i << std::endl;
  });


but the above code only prints the newly added element to an EMPTY vector, which is also mentioned by TheToaster later on.

and then others came with auto range-based and while loop samples along with samples that include pointers. i really appreciated those.

and then mbozzi established that i cannot append new elements to a filled vector and then concurrently print the original + appended vector elements
Lets look at:
1
2
std::vector<std::string> pokemon {"Pikachu", "Charmander"};
std::vector<std::string> to_add {"Butterfree", "Onyx", "Arbok"};

* for_each() could iterate over pokemon. Both 2 elements (or less).
* for_each() could iterate over to_add. All 3 elements (or less).
But
* To print the 5 elements of a vector with for_each() you do need a vector that already has 5 elements.

One could "fake it":
1
2
pokemon.resize( pokemon.size() + to_add.size() );
// pokemon == {"Pikachu", "Charmander", "", "", ""} 

Now for_each() sees 5 elements in pokemon.
Trouble is, how does a function, that merely receives a string s, know whether it should copy value from some element of some array to s, or just print the s?

It is infinitely simpler to have two separate loops:
1. Append values
2. Show values
thanks for all your help, people!! i really do appreciate it!! i will be back to ask for more help + clarification on other topics :D

please stay safe <3
Topic archived. No new replies allowed.
Pages: 12