We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,901 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sending a set iterator to output stream

Hello all,

I have a class with a std::set member that I am trying to send to terminal via std::cout. I am trying to achieve this by iterating through the set and dereferencing the iterator. I am unable to achieve this. Anyone able to point me in the right direction please?

Should I be creating a second loop and iterator of type vector<int> and double looping in some way?

Compiler output.

$ make
g++  -c -O -g combo.cpp main.cpp
combo.cpp: In member function ‘int Combo::printCombinations()’:
combo.cpp:25:19: error: no match for ‘operator<<’ in ‘std::cout << it.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = std::vector<int>, std::_Rb_tree_const_iterator<_Tp>::reference = const std::vector<int>&]()’
combo.cpp:25:19: note: candidates are:....... (goes on and on)

my class and comparision function used for my std::set.

class Compare
{
public:
bool operator()(std::vector<int> n1, std::vector<int> n2)
{
if(n1.size() < n2.size() )
return true;
else
return false;
}
};


class Combo{
public:
  // constructors
  Combo();
  Combo(std::vector<int> & vin):numbers(vin){};

  // getter setter  
  int setNumbers(std::vector<int> vin);
  std::vector<int> getNumbers();
// the all important   
  int findCombo(std::vector<int> n_in);
  int printCombinations();
private:
  std::vector<int> numbers;
  std::set<std::vector<int>,Compare> combinations;
};

My function that is causing the problem

int Combo::printCombinations(){

  std::set<std::vector<int>,Compare>::iterator it;

  for( it = combinations.begin(); it != combinations.end(); it++ ) {
    std::cout << *it << std::endl;   //<------ This is line 25 where I can't output the values of my set!
  }
}

Thanks for any help.

3
Contributors
4
Replies
22 Hours
Discussion Span
5 Months Ago
Last Updated
5
Views
TheBrick
Newbie Poster
19 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Since the set holds a vector you need to add a second for loop inside your first for loop. In this second loop you need to print out each element of the vector since the vector does not overload the << operator.

NathanOliver
Posting Virtuoso
1,515 posts since Apr 2009
Reputation Points: 281
Solved Threads: 277
Skill Endorsements: 3

Thanks, just as I started this discussion I thought of that but didn't have time to try it.

I've tried three apporaches but none of them seem quite right.

My initaial thoughts where

std::set<std::vector<int>,Compare>::iterator it;

  for( it = combinations.begin(); it != combinations.end(); it++ ) {
    std::vector<int>::iterator it2;
    for ( it2 = it->begin(); it2 != it->end(); ++it2 ){
      std::cout << *it2 << std::endl;
    }

which failed on

$ make
g++  -c -O -g combo.cpp main.cpp
combo.cpp: In member function ‘int Combo::printCombinations()’:
combo.cpp:26:27: error: no match for ‘operator=’ in ‘it2 = it.std::_Rb_tree_const_iterator<_Tp>::operator-> [with _Tp = std::vector<int>, std::_Rb_tree_const_iterator<_Tp>::pointer = const std::vector<int>*]()->std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>, std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::const_pointer = const int*]()’

So I thougt, "Ah, itterator it needs to be derefferenced. This will give me the vector<int> which I can then find the start and finish iterators of the vector!"

std::set<std::vector<int>,Compare>::iterator it;

  for( it = combinations.begin(); it != combinations.end(); it++ ) {
    std::vector<int>::iterator it2;
    for ( it2 = (*it).begin(); it2 != (*it).end(); ++it2 ){
      std::cout << *it2 << std::endl;
    }

  }

Also a fail with the same compiler message.

So then out of desperation I tried something which I didn't expect to work.

  std::set<std::vector<int>,Compare>::iterator it;

  for( it = combinations.begin(); it != combinations.end(); it++ ) {
    std::vector<int>::iterator it2;
    for ( it2 = (*it)->begin(); it2 != (*it)->end(); ++it2 ){
      std::cout << *it2 << std::endl;
    }

  }

and got this error, which supprises me less.

make
g++  -c -O -g combo.cpp main.cpp
combo.cpp: In member function ‘int Combo::printCombinations()’:
combo.cpp:26:22: error: base operand of ‘->’ has non-pointer type ‘const std::vector<int>’
combo.cpp:26:45: error: base operand of ‘->’ has non-pointer type ‘const std::vector<int>’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:27:3: error: expected ‘;’ before ‘exit’
make: *** [main.o] Error 1

Can someone offer me some illumination please.

TheBrick
Newbie Poster
19 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The std::set container is a sorted container which means that it will not allow you to modify the elements that it contains, because that could break the ordering. This means that set iterators will only give you const references to the elements of the set, i.e., (*it) is a const reference to a vector of ints. And a const-vector can only provide const-iterators to its elements. And the error you got is a result of trying to assign a const-iterator to an non-const iterator (it2). This should work:

std::set<std::vector<int>,Compare>::iterator it;

  for( it = combinations.begin(); it != combinations.end(); it++ ) {

    std::vector<int>::const_iterator it2;   // notice the 'const_iterator' here.
    for ( it2 = it->begin(); it2 != it->end(); ++it2 ){
      std::cout << *it2 << std::endl;
    }
  }
mike_2000_17
21st Century Viking
Moderator
3,135 posts since Jul 2010
Reputation Points: 2,050
Solved Threads: 625
Skill Endorsements: 41

Perfect thanks. I knew there must be something simple I was missing.

TheBrick
Newbie Poster
19 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0712 seconds using 2.8MB