I want to print out the vector element in a set container. I did the code as follows:
int main() {
vector<int> aa = {3, 2, 1, 1};
vector<int> bb = {5, 1, 7, 9};
set<vector<int>> myset; // setVector
myset.insert(aa);
myset.insert(bb);
for (auto elem : myset) {
cout << elem << ", ";
}
return 0;
}
However, this code can not print out the vector: (3, 2, 1, 1) and (5, 1, 7, 9).
you should also loop your vector elements inside myset.
for (auto const &elem : myset) { // loop set elements
for (auto const &v: elem) { // loop vector elements
std::cout << v << ", "; // print each vector element
}
std::cout << std::endl;
}
auto elem: myset here elem refers to the vectors.
to print out the contents of the vectors do this:
for (auto elem : myset)
{
for(auto x:elem) // elem is each vector
{
std::cout << x << " ";
}
std::cout << std::endl;
}
Here you iterate over the vectors in the inner for loop.
Also, you might want to use auto& in the loop if you are updating elements or to prevent copies since then you get a reference.
In order to print which you tried. You should overload << operator.
Also you may use like this.
for (auto elem : myset) {
cout << "(";
for(auto item:elem)
{
cout << item << ",";
}
cout << ")";
cout << endl;
}
Related
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, int> ma;
// 원소를 추가 하자!!
ma.insert(make_pair(100, 2)); // key 값 : 1 , value : 3
ma.insert(make_pair(101, 3)); // key 값 : 3, value : 13
ma.insert(make_pair(102, 2)); // key 값 : 3, value : 13
ma.insert(make_pair(103, 3)); // key 값 : 3, value : 13
ma.insert(make_pair(104, 1)); // key 값 : 3, value : 13
// make_pair 형식으로 저장 했으므로
// key 값은 fisrt 로 접근 value 값은 second 로 접근한다.
for (auto iter : ma) {
cout << "key : " << iter.first << " value : " << iter.second << '\n';
iter.second = iter.second + 1;
}
cout << "\n" << "\n";
for (auto iter : ma) {
cout << "key : " << iter.first << " value : " << iter.second << '\n';
}
cout << "\n" << "\n";
return 0;
}
Actually , I want to change the value of second component of pair.
iter.second = iter.second + 1;
This code can't change the thing...
How can I change ???
The auto keyword in C++ always tries to infer a non-reference type, so in your case it infers iter as being type std::pair<const int, int>. This is not a reference type, so the values from the map get copied into the iter variable, and so any changes made to iter are not reflected in the map itself.
You can write auto& instead to force an lvalue-reference type:
for (auto& iter : ma) { // <-- Notice ampersand here
iter.second = iter.second + 1;
}
Your main problem is that you called the variable "iter" so you must think it is an iterator of some sort. If it were an iterator, your code would work fine:
#include <iostream>
#include <map>
int main() {
std::map<int, int> map {
{100, 2}, {101, 3}, {102, 2}, {103, 3}, {104, 1},
};
for (auto iter = map.begin(), end = map.end(); iter != end; ++iter) {
std::cout << "key : " << iter->first << " value : " << iter->second << '\n';
++(iter->second);
}
std::cout << "\n\n";
for (auto iter = map.begin(), end = map.end(); iter != end; ++iter) {
std::cout << "key : " << iter->first << " value : " << iter->second << '\n';
}
std::cout << "\n\n";
}
(See online)
But since you are using a range-based for loop you are accessing the values themselves, not an iterator to them. In that case, if you use auto you are getting a copy of the elements, and any changes you make are done the copies, not the elements themselves. If you instead use auto& you will get a reference instead, which is what you want:
#include <iostream>
#include <map>
int main() {
std::map<int, int> map {
{100, 2}, {101, 3}, {102, 2}, {103, 3}, {104, 1},
};
for (auto& [key, val] : map) {
std::cout << "key : " << key << " value : " << val << '\n';
++val;
}
std::cout << "\n\n";
for (auto& [key, val] : map) {
std::cout << "key : " << key << " value : " << val << '\n';
}
std::cout << "\n\n";
}
(See online)
Do note the use of structured bindings to simplify the code.
I have structure:
std::map<std::string, std::vector > someValues;
How can I print all values like:
1, string
2, values from vector?
I have more maps in my c++ project and i need some loops to printf all values.
This might work for what you want.
for (const auto& [key, vec] : someValues) {
std::cout << key << std::endl;
for (const auto& val : vec) {
std::cout << " " << val << std::endl;
}
}
I tried on my own. But I was unable to do it. So, please help.
unordered_map<string, pair<string , vector<int>>> umap;
Or more precisely, how We can make pair of one string and one vector that can be used in map.
Well you can use insert function and insert them as a pair (Or precisely nested pairs).
For example Checkout this program :
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string, pair<string , vector<int>>> umap;
//Insert like this
umap.insert(make_pair("first", make_pair("data1",vector<int>(3, 0))));
umap.insert(make_pair("second", make_pair("data2",vector<int>(3, 1))));
//or like this
string s = "new", t= "method";
vector <int> v = {1, 2, 3};
umap.insert(make_pair(s, make_pair(t, v)));
//This will print all elements of umap
for(auto p : umap)
{
cout << p.first << " -> " << p.second.first << " , VECTOR : " ;
for(auto x : p.second.second)
cout << x << ',';
cout << endl;
}
cout << endl;
//Let's see how to change vector already inside map
auto itr = umap.begin();
cout << "BEFORE : ";
for(auto x : (itr->second).second)
{
cout << x << ',';
}
cout << endl;
//We will push 42 here
(itr->second).second.push_back(42);
cout << "AFTER : ";
for(auto x : (itr->second).second)
{
cout << x << ',';
}
cout << endl;
}
Output Is :
new -> method , VECTOR : 1,2,3,
second -> data2 , VECTOR : 1,1,1,
first -> data1 , VECTOR : 0,0,0,
BEFORE : 1,2,3,
AFTER : 1,2,3,42,
I hope this helps.
This depends a lot on the complexity of what you are creating. For example, if you have some constants in your vector, you could make them in place:
umap.emplace("s", std::make_pair("s2", std::vector<int>{1, 2, 3, 4}));
It is more likely however that you will be making the internals in some complex way. In which case you could more easily do it as separate constructions.
std::vector<int> values;
values.push_back(1);
auto my_value = std::make_pair("s2", std::move(values));
umap.emplace("s2", std::move(my_value));
Using move to move the data around ensures minimal copying.
Finally, to iterate the items, it is normal to use range-based for loops:
for (const auto& [key, value]: umap) {
std::cout << key << ": ";
const auto& [name, values] = value;
std::cout << name << " {";
for (auto val : values) {
std::cout << val << " ";
}
std::cout << "}\n";
}
Here you can check out a live example.
I try to display the first elements of a std::set. For each time I would like to display the remaining elements.
For example a set: 4 6 8 9.
the first element is 4 the rest of the set is 6 8 9.
the first two elements are 4 6 the rest is 8 9.
then the first three elements are 4, 6, 8 the rest: 9.
The code I begun with:
for (std::set<int>::iterator it = my_set.begin(); it != my_set.end(); ++it)
{
std::vector<decltype(*it)> items((my_set.begin()), *it);
// items is a vector containing successively the n first elements
}
With this code I have errors and I can't have the rest of the set after displaying the first elements.
Can you help me?
The vector constructor you are looking for is the one taking iterators:
std::vector<std::decay_t<decltype(*it)>> items(my_set.begin(), it);
But you don't need intermediate std::vector, you might directly work with the range (with iterators)
You might use something like:
template <typename IT>
void print(IT begin, IT end)
{
const char* sep = "";
for (auto it = begin; it != end; ++it) {
std::cout << sep << *it;
sep = ",";
}
}
int main()
{
std::set<int> s{4, 6, 8, 9};
for (auto it = std::next(s.begin()); it != s.end(); ++it) {
std::cout << "First elements: "; print(s.begin(), it);
std::cout << ", Rest: "; print(it, s.end()); std::cout << std::endl;
}
}
Demo
If you want independent functions for N first elements and the rest:
#include <set>
#include <iostream>
using namespace std;
template<typename T>
void printHead(set<T>& mySet, unsigned int position) {
for (auto it = mySet.begin(); it != next(mySet.begin(), position); it++) {
cout << *it << ' ';
}
cout << endl;
}
template<typename T>
void printTail(set<T>& mySet, unsigned int position) {
for (auto it = next(mySet.begin(), position); it != mySet.end(); it++) {
cout << *it << ' ';
}
cout << endl;
}
int main(void) {
set<int> foo{0, 1, 2, 3, 4, 5, 6, 7};
const auto N = 3;
cout << "First " << N << " elements: ";
printHead(foo, N);
cout << "The rest: ";
printTail(foo, N);
}
I have a vector array called nVectors.
vector<int>* nVectors[21];
for (int i = 1; i <= 20; i ++) {
nVectors[i] = generateVector(i);
}
I can print all the members of a single vector, but when it comes to the vector array, I still don't know how to print all the vectors in an array.
Maybe an iterator through all the member of a vector array and print using my predefined method pvector can solve this problem? But I don't know how to iterate in gdb.
std::array<std::vector<int>*, 21> nVectors;
for(std::array<std::vector<int>*>::iterator i = nVectors.begin();
i != nVectors.end();
++i)
{
for(std::vector<int>::iterator it = (*i)->begin();
it != (*i)->end();
++it)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;
Or, in C++11:
std::vector<int>* nVectors[21];
for(auto &i : nVectors)
{
for(auto &it : i)
{
std::cout << *it << " ";
}
}
std::cout << std::endl;