user2868657
user2868657

Reputation:

Vector of subsets

I have the next program.How should I use the iterator in main in order to show the subsets that have the sum 0?
My program should print:
2 -2
5 -5

# include < iostream >
# include < vector >

using namespace std;

vector < vector < int > > test(vector <int> data)
{
    vector <int> a;
    vector < vector < int > > ret;
    vector <int> :: iterator it1;
    vector <int> :: iterator it2;
    int i=0;
    for(it1 = data.begin(); it1!= data.end(); it1++,i++)
    {
        for(it2 = data.begin() + i; it2!= data.end(); it2++ )
        {
            if( *it1 + *it2 == 0)
            {
                cout<<*it1<<" "<<*it2<<"\n";
                a.push_back(*it1);
                a.push_back(*it2);
                ret.push_back(a);
                a.clear();
            }
        }
    }
    return ret;
}
int main()
{
    vector < int > data;
    data.push_back(2);
    data.push_back(5);
    data.push_back(-3);
    data.push_back(-2);
    data.push_back(-5);
    vector <vector <int > > v=test(data);
    //how to continue printing the subsets

}

Upvotes: 0

Views: 132

Answers (3)

P0W
P0W

Reputation: 47794

std::vector<std::vector<int> >::iterator it=v.begin(), end=v.end();
for ( it!= end, ++it) {
  std::copy(it->begin(),it->end(),std::ostream_iterator<int>(std::cout, " "));
}

Or using c++11

 for(const auto& i:v)
 { 
   for(const auto& j:i)
     std::cout<<j<< " ";
   std::cout<<std::endl;
 }

Upvotes: 1

Vadiklk
Vadiklk

Reputation: 3764

Added

#include <iostream>
#include <vector>

using namespace std;

vector < vector < int > > test(vector <int> data)
{
    vector <int> a;
    vector < vector < int > > ret;
    vector <int> :: iterator it1;
    vector <int> :: iterator it2;
    int i=0;
    for(it1 = data.begin(); it1!= data.end(); it1++,i++)
    {
        for(it2 = data.begin() + i; it2!= data.end(); it2++ )
        {
            if( *it1 + *it2 == 0)
            {
                cout<<*it1<<" "<<*it2<<"\n";
                a.push_back(*it1);
                a.push_back(*it2);
                ret.push_back(a);
                a.clear();
            }
        }
    }
    return ret;
}
int main()
{
    vector < int > data;
    data.push_back(2);
    data.push_back(5);
    data.push_back(-3);
    data.push_back(-2);
    data.push_back(-5);
    vector <vector <int > > v=test(data);
    //how to continue printing the subsets

    // here is how:
    for (vector <vector <int > >::iterator it = v.begin(); it != v.end(); ++it) {
        vector<int> v = *it;
        for (vector<int>::iterator it2 = v.begin(); it2 != v.end(); ++it2) {
            cout << *it2 << " ";
        }
        cout << endl;
    }
}

Upvotes: 0

Jay Nebhwani
Jay Nebhwani

Reputation: 976

Roughly in pseudocode:

foreach int i in data:
  foreach int j in data not equal to i:
     if (i + j = 0)
       print(i, j)

The problem with this code is that it will print repeated elements:

2 -2 -2 2 5 -5 -5 5

You probably don't want this behaviour but you can improve it later on. For now this is a good starting point.

Upvotes: 0

Related Questions