Error when trying to print out values from vectors

Hi , just wanted to ask.. because i was trying to access some data from my vector container. i was using the code below to access it

for (std::vector<string>::const_iterator i = events.begin(); i != events.end(); ++i)
std::cout << *i << ""<<endl;

the funny thing is, it works in my other program . but it does not work on the other. the error is below: sorry the error code is quite long
please advise, as i am new to this

/usr/include/c++/4.9/bits/streambuf_iterator.h:210:5: note: template argument deduction/substitution failed:
IDS.cpp:143:74: note: ‘std::vector<std::basic_string<char> >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ is not derived from ‘const std::istreambuf_iterator<_CharT, _Traits>’
for (vector<string>::const_iterator i = events.begin(); i != events.end(); ++i)
^
In file included from /usr/include/i386-linux-gnu/c++/4.9/bits/stdc++.h:66:0,
from IDS.cpp:19:
/usr/include/c++/4.9/complex:471:5: note: template<class _Tp> bool std::operator!=(const std::complex<_Tp>&, const std::complex<_Tp>&)
operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
^
/usr/include/c++/4.9/complex:471:5: note: template argument deduction/substitution failed:
IDS.cpp:143:74: note: ‘std::vector<std::basic_string<char> >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ is not derived from ‘const std::complex<_Tp>’
for (vector<string>::const_iterator i = events.begin(); i != events.end(); ++i)
^
In file included from /usr/include/i386-linux-gnu/c++/4.9/bits/stdc++.h:66:0,
from IDS.cpp:19:
/usr/include/c++/4.9/complex:476:5: note: template<class _Tp> bool std::operator!=(const std::complex<_Tp>&, const _Tp&)
operator!=(const complex<_Tp>& __x, const _Tp& __y)
^
/usr/include/c++/4.9/complex:476:5: note: template argument deduction/substitution failed:
IDS.cpp:143:74: note: ‘std::vector<std::basic_string<char> >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >}’ is not derived from ‘const std::complex<_Tp>’
for (vector<string>::const_iterator i = events.begin(); i != events.end(); ++i)
^

Hi,

The best way to do this (seen as you want to print the whole container) is to use a range based for loop:


1
2
3
for (const auto& event : events) {
     std::cout << event << "\n";
}


event is one of the items in the container events.

The const says that individual event items will not be modified.

The auto automagically determines the type, in this case std::vector<string>::const_iterator. which saves a lot of typing and is less error prone.

The & means the values are passed by reference, which is an efficient way of doing things.

Good luck !!
The auto automagically determines the type, in this case std::vector<string>::const_iterator. which saves a lot of typing and is less error prone.

To me it looks event is a string not a iterator.
If you write
1
2
3
4
for (const string& event : events) 
{
     std::cout << event << "\n";
}

things would be clear.
thanks thanks for the advise , this seems to come in handy. but im not sure is this is compatible to use with my g++ compiler as i am using ubuntu

error: ISO C++ forbids declaration of ‘event’ with no type [-fpermissive]
for (const auto& event : events) {
To me it looks event is a string not a iterator.

Ok, my bad :o)

But I still think it's a good idea to use auto here. It's less error prone - Haha :+D Especially if the container contains something complicated.
Why is your error message referring to
std::complex<>
?

How is the container events declared in your program? Is it declared as vector<string> or something else?
OMG ! thanks i just noticed that i declared it as
vector<vector<string>>, so now that part is working but now i have a problem with the cout

for (vector<vector<string> >::iterator i = events.begin(); i != events.end(); ++i)
{
cout << *i << ""<<endl;
}


having error with the cout part
*i would return a vector<string>, for which the << operator is not defined in the standard (though you could write one easily enough).

Alternatively, (untested - may be subject to typos):
1
2
3
4
5
for ( const vector<string> &row : events )
{
   for ( const string &s : row ) cout << s << " ";
   cout << '\n';
}
Last edited on
Thanks for your suggestion, but im not sure if this is compatable with g++ compiler that im using. i need to do it in ubuntu . there is an error of

error: range-based ‘for’ loops are not allowed in C++98 mode
Don't run it in C++98 mode.

Try compiling with compiler flag -std=c++11

As a last resort (and, again, untested):
1
2
3
4
5
for ( int row = 0; row < events.size(); row++ )
{
   for ( int col = 0; col < events[row].size(); col++ ) cout << events[row][col] << " ";
   cout << '\n';
}
Last edited on
sorry newbie,

what do you mean by
Try compiling with compiler flag -std=c++11
If you were running from command line (or linux shell) I would type something like
g++ -std=c++11 mycode.cpp

If you are using some IDE then you will have to consult its documentation to find out how to pass a compiler option. I don't use any IDE.

I have edited my previous post to do the output the C++98 way.
OMG !! it works well now. Thank you very much for your help ! I appreciate it :)
Hi,

Consider upgrading the compiler to the most recent version. It may be worth upgrading the version of Ubuntu that you have too . Later versions of gcc use -std=c++14 by default. I always try to have the latest of everything.

Have a look at this:

http://www.cplusplus.com/forum/unices/227621/#msg1036065



When compiling always use a high level of warnings:

g++ -std=c++14 -Wall -Wextra -pedantic-errors *.cpp -o MyProg

MyProg is the name of the executable file you want to have.

Warnings are your friend, they tell you about problems with your program. Even if it compiles without errors, the warnings mean there will be problems with how the program runs. Ideally one isn't finished until there are no warnings.

Using auto with a nested vector:

1
2
3
4
5
6
for ( const auto& row : events )
{
   for ( const auto& s : row ) { std::cout << s << " "; 
   }
   std::cout << '\n';
}
Thank you very much everyone.
Last edited on
Topic archived. No new replies allowed.