i would like to do a thing like this. can ou help me :) Thank you
/* Example: */
struct Name
{
const char *full_name;
const char *name;
};
std::vector<Name> n = { {"Harry Potter", "Harry"}, {"Hermione Granger", "Hermione"} };
// The expressions below does not work
string::const_iterator b = n.begin();
string::const_iterator e = n.end();
int s = n.size();
// ...
The problem mainly comes from the :
string::const_iterator b = n.begin(); // not possible
string::const_iterator e = n.end(); // not possible
int s = n.size(); // working as intended if you print it to the console
If you want to declare an iterator, in this case, you could do :
std::vector<Name>::const_iterator it;
If you would like to have control with the iterator, use it in a for loop :
for (it = n.begin(); it != n.end() - 1; it++) // Will stop at the first name in our example
{
std::cout << it->full_name << std::endl;
}
As it is pointing to n, in order to access its values, don't forget to use ->.
If you wanted to iterate through the whole vector, it is possible to use a simple loop.`
for (auto i: n)
{
std::cout << "My name is " << i.name << ", ";
std::cout << i.full_name << std::endl;
}`
On a side note, you did not need to declare "it" before it was used in the for loop.
The following would work too :
for (auto it = n.begin(); it != n.end() - 1; it++) // Will stop at the first name in our example
{
std::cout << it->full_name << std::endl;
}
For reference, here is my final code with examples, I hope that solves your problem :
struct Name
{
std::string full_name;
std::string name;
};
int main()
{
std::vector<Name> n = { {"Harry Potter", "Harry"}, {"Hermione Granger", "Hermione"} };
std::vector<Name>::const_iterator it;
for (auto i : n) // Printing the whole vector
{
std::cout << "My name is " << i.name << ", ";
std::cout << i.full_name << std::endl;
}
for (it = n.begin(); it != n.end() - 1; it++) // will return all the names except the last
{
std::cout << it->full_name << std::endl;
}
for (auto g = n.begin(); g != n.end() - 1; g++) // same as it, iterator initialized in the loop
{
std::cout << g->full_name << std::endl;
}
return 0;
}
Related
As the title says I want to print out all the elements of "group1" in this 2D map. I tried this but I just get a huge error at the for loop.
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main(){
map<string,map<int,double> > myMap;
myMap["group1"][13] = 10.41;
myMap["group1"][15] = 31.2;
//print all elements in "group1"
for (map< string, map<int,double> >::const_iterator iter =
myMap["group1"].begin(); iter != myMap["group1"].end(); ++iter)
{
cout << iter->first << '\t' << iter->second << '\n';
}
return 0;
}
Any help would be appreciated, thanks!
The type of myMap["group1"] is map<int,double> and not map<string,map<int,double>>
Then your code would look somehow like this:
const map<int,double> &grp1map = myMap["group1"];
for (map<int,double>::const_iterator iter = grp1map.begin(); iter != grp1map.end(); ++iter)
{
cout << iter->first << '\t' << iter->second << '\n';
}
You can simplify this with auto:
auto &grp1map = myMap["group1"];
for (auto iter = grp1map.begin(); iter != grp1map.end(); ++iter)
{
cout << iter->first << '\t' << iter->second << '\n';
}
But anyway you should do it like this:
for (auto &elem : myMap["group1"] )
{
cout << elem.first << '\t' << elem.second << '\n';
}
You want an iterator to your internal map<int,double>, whereas you are trying to get and iterator to your outer map<string,map<int,double> >:
int main() {
map<string, map<int, double> > myMap;
myMap["group1"][13] = 10.41;
myMap["group1"][15] = 31.2;
//print all elements in "group1"
for (map<int, double>::const_iterator iter =
myMap["group1"].begin(); iter != myMap["group1"].end(); ++iter)
{
cout << iter->first << '\t' << iter->second << '\n';
}
return 0;
}
Condition based for loop selection.
if(valid)
for (std::multimap<int,int>::reverse_iterator rit=id_count.rbegin(); mcount<10 && rit!=id_count.rend();++rit)
else
for (std::multimap<int,int>::iterator rit=id_match.begin(); mcount<10 && rit!=id_match.end();++rit)
{
//this is common for both for loop
}
how to achieve this in C++?
You have no choice but putting the common part in a function, very roughly like this:
void somefunction(...)
{
//this is common for both for loops
}
if (valid)
{
for (std::multimap<int,int>::reverse_iterator rit=id_count.rbegin(); mcount<10 && rit!=id_count.rend();++rit)
somefunctiuon(...);
}
else
{
for (std::multimap<int,int>::iterator rit=id_match.begin(); mcount<10 && rit!=id_match.end();++rit)
somefunctiuon(...);
}
This is arguably most useful as an illustration that it's not worth combining the loop logic, though it does work. Provided here for interest value...
#include <iostream>
#include <map>
int main()
{
std::multimap<int,int> id_count = { {1,2}, {9, -2}, {1,44}, {2,3}, {3,5}, {7,34} };
for (int valid = 0; valid < 2; ++valid)
{
std::cout << "valid " << valid << '\n';
int mcount = 0;
for (std::multimap<int,int>::iterator it = valid ? id_count.rbegin().base()
: id_count.begin();
mcount<10 && (valid ? it--!=id_count.begin() : it!=id_count.end());
(valid ? it : ++it), ++mcount)
{
std::cout << "[mcount " << mcount << "] "
<< it->first << ',' << it->second << '\n';
}
std::cout << '\n';
}
}
You can create a template function:
#include <map>
#include <iostream>
template<typename I> void func(I begin, I end) {
int mcount = 0;
for (I it = begin; mcount < 10 && it != end; ++it) {
++mcount;
std::cout << "[mcount " << mcount << "] "
<< it->first << ',' << it->second << '\n';
}
}
int main() {
std::multimap<int,int> id_count = { {1,2}, {9, -2}, {1,44}, {2,3}, {3,5}, {7,34} };
for (int valid = 0; valid < 2; ++valid) {
std::cout << "valid " << valid << '\n';
if (valid) {
func(id_count.rbegin(), id_count.rend());
} else {
func(id_count.begin(), id_count.end());
}
std::cout << '\n';
}
}
But IMHO this solution is a bit complicated, so consider other ways (like placing the loop body in a function).
You can try "#if valid", like:
#if 0
for(i=1;i<10;++i)
#else
for(i=2;i<9;++i)
#endif
{
cout << i << endl;
}
In C++14 you also have an option to use generic lambdas:
auto common_code = [/* Capture state if needed */] ( auto& Iter )
{
// Your common code
};
if ( valid )
for ( std::multimap<int, int>::reverse_iterator rit = id_count.rbegin(); mcount < 10 && rit != id_count.rend(); ++rit )
common_code( rit );
else
for ( std::multimap<int, int>::iterator rit = id_match.begin(); mcount < 10 && rit != id_match.end(); ++rit )
common_code( rit );
Just a question, on the use of const_iterator vs just iterator. More specifically with the use of distance(). Below is some basic code that just craps out a list of "fav_games" that the user enters (earlier in the application). I wanted to also crap out the 'index' of the vector so as to print out a numbered list.
Partial Code:
int main()
{
vector<string> fav_games;
vector<string>::const_iterator iter; // const_iterator no worky with "distance()"
if (fav_games.size() > 0) {
cout << "\n\nCurrent game list: \n";
for (iter = fav_games.begin(); iter != fav_games.end(); ++iter)
{
cout << distance(fav_games.begin(), iter) << ". " << *iter << endl;
}
}
return 0;
}
My question is why, "const_iterator" will not work, where I am forced to use "iterator" instead. Looking for the 'theory' behind it. "distance()" appears to expecting and "iterator" -- not "const_iterator". ..Why?
Just for reference the compile error if I use "const_iterator":
Error 1 error C2782: 'iterator_traits<_Iter>::difference_type std::distance(_InIt,_InIt)' : template parameter '_InIt' is ambiguous z:\micah\c++\favgames\favgames\favgames.cpp 49 1 favgames
Thanks!
Try this instead:
vector<string>::const_iterator b, iter, e;
if (fav_games.size() > 0) {
cout << "\n\nCurrent game list: \n";
for (b = fav_games.begin(), iter = b, e = fav_games.end(); iter != e; ++iter)
{
cout << distance(b, iter) << ". " << *iter << endl;
}
}
distance has no problem with two const_iterator instances, or two iterator instances. Your mistake was in mixing them.
Still, making O(n) calls to distance is crazy. Just use a counter:
vector<string>::const_iterator iter, e;
size_t i;
if (fav_games.size() > 0) {
cout << "\n\nCurrent game list: \n";
for (i = 0, iter = fav_games.begin(), e = fav_games.end(); iter != e; (++iter), (++i))
{
cout << i << ". " << *iter << endl;
}
}
In C++11 and later, one may of course go one step further and avoid explicit use of the iterators at all:
if (fav_games.size() > 0) {
int i = 0;
cout << "\n\nCurrent game list: \n";
for (const string& game : fav_games)
{
cout << (i++) << ". " << game << endl;
}
}
I am trying to write a print function for set in C++, and this is what I wrote :
void print_set(set<int> &s)
{
cout<<"{";
for(auto it = s.begin() ; it!=s.end() ; ++it)
{
cout<<*it;
if(it!=(s.end()-1)) //shows error here
cout<<",";
}
cout<<"}";
}
But I am getting error. How can I check whether current element is last element or not ?
set's integers are not random-access, so you can't do arithmetic on them. Use std::prev instead.
May I suggest an alternative approach?
Print the comma before each element, except the first:
void print_set(set<int> &s)
{
cout << "{";
for(auto it = s.begin() ; it != s.end() ; ++it)
{
if(it != s.begin())
cout << ", ";
cout << *it;
}
cout << "}";
}
Simply check whether the next element equals the end:
auto next = it;
++next;
if (next != s.end())
cout << ",";
You can only apply the ++ and -- operators on set iterators. Adding a number is not defined. You can make your code work like so:
void print_set(set<int> &s)
{
cout<<"{";
auto second_to_last = s.end();
if (!s.empty()) {
second_to_last--;
}
for(auto it = s.begin() ; it!=s.end() ; ++it)
{
cout<<*it;
if(it!=second_to_last) {
cout<<", ";
}
}
cout<<"}";
}
What this code does is essentially store an iterator to the second to last element once and then compare the element you have with it. Please note that second_to_last will not be accurate if the set is empty but the code will still work as expected.
What I do in this case:
void print_set(const std::set<int>& s)
{
const char* sep = "";
std::cout << "{";
for (int e : s)
{
std::cout << sep << e;
sep = ", ";
}
std::cout << "}";
}
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;