STL iterator question

T

Thomas

In my little program I am using a set<vector<int> > rather than a
vector<vector<int> > because the program produces many duplicates, and
the set seems to be just the right type of container to keep them out.

I'm having trouble sending the contents of the set to my screen or to a
file - don't know how to refer correctly to the integers in the vectors:

set<vector<int> > svint;
vector<int> vint;
..
..
svint.insert (vint); etc.
..
..
for (set<vector<int> >::iterator svi = svint.begin();
svi != svint.end(); ++svi)
{
for (vector<int>::iterator vi = ?; vi != ?; ++vi)
cout << ? << " ";
cout << endl;
}

I know that in a vector<int> you can refer to the elements by using *vi,
but I don't know how to proceed now that the vectors are embedded in a
std::set. All help appreciated!

T.
 
R

RaZiel

In my little program I am using a set<vector<int> > rather than a
vector<vector<int> > because the program produces many duplicates, and
the set seems to be just the right type of container to keep them out.

I'm having trouble sending the contents of the set to my screen or to a
file - don't know how to refer correctly to the integers in the vectors:

set<vector<int> > svint;
vector<int> vint;
.
.
svint.insert (vint); etc.
.
.
for (set<vector<int> >::iterator svi = svint.begin();
svi != svint.end(); ++svi)
{
for (vector<int>::iterator vi = ?; vi != ?; ++vi)
cout << ? << " ";
cout << endl;
}

I know that in a vector<int> you can refer to the elements by using *vi,
but I don't know how to proceed now that the vectors are embedded in a
std::set. All help appreciated!

T.

for (set<vector<int> >::iterator svi = svint.begin();
svi != svint.end(); ++svi)
{
for (vector<int>::iterator vi = svi->begin(); vi != svi->end(); ++vi)
cout << *vi << " ";
cout << endl;
}

hth
 
V

Victor Bazarov

In my little program I am using a set<vector<int> > rather than a
vector<vector<int> > because the program produces many duplicates, and
the set seems to be just the right type of container to keep them out.

I'm having trouble sending the contents of the set to my screen or to a
file - don't know how to refer correctly to the integers in the vectors:

set<vector<int> > svint;
vector<int> vint;
.
.
svint.insert (vint); etc.
.
.
for (set<vector<int> >::iterator svi = svint.begin();
svi != svint.end(); ++svi)
{

const vector said:
for (vector<int>::iterator vi = ?; vi != ?; ++vi)

..vi = vv.begin(); vi != vv.end(); ++vi)
cout << ? << " ";

cout << *vi << ' ';
cout << endl;
}

I know that in a vector<int> you can refer to the elements by using *vi,
but I don't know how to proceed now that the vectors are embedded in a
std::set. All help appreciated!

V
 
T

Thomas

Thank you very much, RaZiel and Victor Bazarov.

Both of your solutions, which I reproduce below in miniprograms to
eliminate other possible causes, give the same compiler error, which I
do not understand:

[C++ Error] wisseldata.cpp(16): E2034 Cannot convert 'const int *' to
'int *'

----------------------------------------
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
set<vector<int> > svint;
int a[] = {1,2,3};
vector<int> vint(a,a+3);
svint.insert(vint);
for (set<vector<int> >::iterator svi = svint.begin();
svi != svint.end(); ++svi)
{
for (vector<int>::iterator vi = svi->begin();
vi != svi->end(); ++vi)
cout << *vi << " ";
cout << endl;
}
return 0;
}
-------------------------------------------
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
set<vector<int> > svint;
int a[] = {1,2,3};
vector<int> vint (a, a+3);
svint.insert (vint);
for (set<vector<int> >::iterator svi = svint.begin();
svi != svint.end(); ++svi)
{
const vector<int>& vv = *svi;
for (vector<int>::iterator vi = vv.begin();
vi != vv.end(); ++vi)
cout << *vi << ' ';
cout << endl;
}
return 0;
}

T.
 
Ö

Öö Tiib

Thank you very much, RaZiel and Victor Bazarov.
-------------------------------------------
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
     set<vector<int> > svint;
     int a[] = {1,2,3};
     vector<int> vint (a, a+3);
     svint.insert (vint);
     for (set<vector<int> >::iterator svi = svint.begin();
             svi != svint.end(); ++svi)
     {
         const vector<int>& vv = *svi;
         for (vector<int>::iterator vi = vv.begin();

vector const provides only const_iterators:
 
T

Thomas

vector const provides only const_iterators:

for (vector<int>::const_iterator vi = vv.begin();

Thank you so much, Öö Tiib! I hope to study the subject soon.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top