mapping in the loop
Is it possible to let the map on a loop and map different string with different vectors? It means that have a conceptual loop as following.
1 2
|
for(i=0, i=10, i++)
map< string_i, vector<int> > myVectors_i;
|
Last edited on
Your 'conceptual loop' creates 10 maps and immediately destroys them. Perhaps you meant something like the following?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <vector>
#include <string>
#include <map>
std::ostream& operator<<(std::ostream&os, const std::vector<int>& v)
{
os << "{ ";
for (auto e : v)
os << e << ' ';
return os << '}';
}
int main()
{
std::map<std::string, std::vector<int>> vecs;
for (int i = 0; i < 10; ++i)
vecs[std::string(1, 'a' + i)] = std::vector<int> { i * 2, i * 2 + 1, i * 2 + 2 };
for (auto& pair : vecs)
std::cout << pair.first << " = " << pair.second << '\n';
}
|
http://ideone.com/JBnrMa
Topic archived. No new replies allowed.