Reputation: 25
I got some problems how to define & use iterators.
This is an ilustration of my code.
set< pair<int, pair<int,int> > > data1;
set< pair<int , pair<int,int> > > iterator it1;
// imagine set < pair x , pair < y , z >>>
vector<set <int> > data2;
set<int>::iterator it2;
vector< pair<int,int> > data3;
vector< pair<int,int> > it3;
How can I display content of data1,data2,data3 with iterators please ? Thanks for any help.
Upvotes: 1
Views: 651
Reputation: 81936
It's fairly straightforward...
#include <iostream>
#include <set>
#include <utility>
#include <vector>
int main() {
std::set<std::pair<int, std::pair<int,int>>> data1;
data1.emplace(3, std::make_pair(4, 5));
data1.emplace(6, std::make_pair(7, 8));
for (auto p : data1)
std::cout << std::get<0>(p) << " " << std::get<0>(std::get<1>(p)) << " " << std::get<1>(std::get<1>(p)) << "\n";
std::cout << "\n";
std::vector<std::set<int>> data2;
data2.push_back({4,5,6});
data2.push_back({7,8,9});
for (auto set_element : data2) {
for (auto element : set_element)
std::cout << element << " ";
std::cout << "\n";
}
std::cout << "\n";
std::vector<std::pair<int,int>> data3;
data3.emplace_back(1, 2);
data3.emplace_back(3, 4);
for (auto p : data3)
std::cout << std::get<0>(p) << " " << std::get<1>(p) << "\n";
std::cout << "\n";
}
Which outputs...
3 4 5
6 7 8
4 5 6
7 8 9
1 2
3 4
Upvotes: 1