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.

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.

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.

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;
    }
  }
commented: Excellent! +0

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.