Reputation: 121
If I have a C++ set and iterator:
set<Person> personList;
set<Person>::const_iterator location;
How can I print out the contents of the set? They are all person objects, and I have overloaded operator<< for Person.
The line that errors is in a basic for loop:
cout << location
Netbeans gives:
proj.cpp:78: error: no match for ‘operator<<’ in ‘std::cout << location’
It looks like it wants an overload for the iterator's operator<<.
Basically, I am taking objects that used to be stored in an array format, but are now in a set. What is the equivalent to cout << array[i]
for sets?
Upvotes: 12
Views: 89188
Reputation: 1
// Online C++ compiler to run C++ program online
#include <iostream>
#include<set>
using namespace std;
struct Car
{
int m;
int p;
void set(int a,int b){
m =a,p = b;
}
};
struct Car cT[50];
int cI=0;
bool operator < (const Car& c1,const Car& c2){
return c1.p < c2.p;
}
ostream& operator << (ostream& o, const Car& a)
{
o << "Car Model: " << a.m << " price: " << a.p << endl;
return o;
}
void insertS(set<Car>& c){
for(int i=0;i<cI;i++){
c.insert(cT[i]);
}
}
void printS(set<Car> c){
set<Car> :: iterator i;
for( i=c.begin();i!=c.end();i++){
cout << *i<<endl;
}
}
int main() {
// Write C++ code here
set<Car> mCs;
cT[cI++].set(4,5);
cT[cI++].set(34,4);
cT[cI++].set(43,6);
cT[cI++].set(41,15);
insertS(mCs);
printS(mCs);
return 0;
}
Output will be:::
Car Model: 34 price: 4
Car Model: 4 price: 5
Car Model: 43 price: 6
Car Model: 41 price: 15
Upvotes: 0
Reputation: 1681
begin() is a built-in function in C++ STL function which used to return an iterator pointing to the first element of the set container.
end() is a built-in function in C++ STL which is used to get an iterator to past the last element. The past-the-end element is the theoretical element that would follow the last element in the vector. It does not point to any element, and thus shall not be dereferenced.
Also, the objects in Set are distinct values stored in ascending or descending order.
example 1:
set<char> myset{'a', 'c', 'g', 'z'};
for (auto it=myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
output : a c g z
example 2:
set<string> myset{"This", "is", "confused"};
for (auto it=myset.begin(); it != myset.end(); ++it)
cout << ' ' << *it;
output : This confused is
Upvotes: 2
Reputation: 106549
In C++11, why use a for loop when you can use a foreach loop?
#include <iostream> //for std::cout
void foo()
{
for (Person const& person : personList)
{
std::cout << person << ' ';
}
}
In C++98/03, why use a for
loop when you can use an algorithm instead?
#include <iterator> //for std::ostream_iterator
#include <algorithm> //for std::copy
#include <iostream> //for std::cout
void foo()
{
std::copy(
personList.begin(),
personList.end(),
std::ostream_iterator(std::cout, " ")
);
}
Note that this works with any pair of iterators, not only those from std::set<t>
. std::copy
will use your user-defined operator<<
to print out every item inside the set
using this single statement.
Upvotes: 24
Reputation: 862
it is a set::iterator
for(it = output_set.begin(); it != output_set.end(); it++)
{
outstream_1 << *it << endl;
}
Upvotes: 6
Reputation: 98974
You need to dereference the iterator:
std::cout << *location;
The convention of using the indirection or dereferencing operator to get the referenced value for an iterator was chosen in analogy to pointers:
person* p = &somePerson;
std::cout << *p;
Upvotes: 7