0

I have this example code that doesn't compile:

#include <iostream>
#include <vector>

using std::endl; using std::cin; using std::cout;
using std::vector;


int main()
{
    vector<int> vi, seg;
    vi.push_back(3);
    vi.push_back(4);
    vi.push_back(5);
    vi.push_back(6);
    vi.push_back(7);
    vector<int>::const_iterator ci_start = vi.begin();
    vector<int>::const_iterator ci_actual = vi.begin();
    while (ci_actual != vi.end())
    {   
        seg = vi(ci_start, ci_actual);
        cout << "......" ;
        for (const vector<int>::const_iterator ci = vi.begin();
                                    ci != vi.end(); ++ci)
            cout << *ci << " ";
        cout << endl;

        ++ci_actual;
    }   
}

What I want is to pass increasing parts of vector vi (that is going to take some million elements) to a function (not shown). These segments are considered from the begining of vi up to where actual is at the moment.
For this, I've declared seg that was supposed to have part of vi.

Desired output would be:

3
3 4
3 4 5
3 4 5 6 3 4 5 6 7

Is seg not supposed to hold a copy of elements of vi denoted by iterators ci_start and ci_actual?

To pass the ever increasing part of vi to the function I refered (absent), can I do it faster without copying the elements of vi to another vector and just pass a reference to a segment of vi?

Luis
  • 1,236
  • 5
  • 22
  • 31

4 Answers4

3
for (const vector<int>::const_iterator ci = vi.begin(); ci != vi.end(); ++ci)

You are declaring a const const_interator here. Meaning whey you try to call ++ci, the compiler will yell at you for trying to modify a constant variable. This should fix that compiler error:

for (vector<int>::const_iterator ci = vi.begin(); ci != vi.end(); ++ci)
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • Well, there were two errors and you solved one. Thank you. But I still get:ex.cpp:23: error: passing 'const __gnu_cxx::__normal_iterator > >' as 'this' argument of '__gnu_cxx::__normal_iterator<_Iterator, _Container>& __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator++() [with _Iterator = const int*, _Container = std::vector >]' discards qualifiers – Luis Dec 30 '10 at 16:42
  • That is likely due to your `seg = vi(ci_start, ci_actual);` line which @Lou pointed out below. – Zac Howland Dec 30 '10 at 16:49
2

If you have a compiler error, please tell us what it is. It looks like this:

  seg = vi(ci_start, ci_actual);

Should be

  vector<int> seg(ci_start, ci_actual);

And remove the declaration of seg at the top of main().

And your for loop should use seg.begin()/seg.end() if you want to print the segment

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • After removing the `const` from the vector declaration in the for cycle, I tryed again your suggestion and the compiler doesn't complain anymore. It seems that there is no problem to keep re-declaring vector seg inside the while loop. Shouldn't the variable be declared only once? – Luis Dec 30 '10 at 16:51
  • It is only declared once. The loop doesn't keep declaring it -- it keeps constructing it, but that's ok because you want a different seg for each iteration. – Lou Franco Dec 30 '10 at 17:10
1

Re: your follow-on question about how to call a helper function that you want to operate on a subrange of your large vector...

First, this stackoverflow question might be useful. The accepted "just pass a pair of iterators" answer was exactly what I was about to suggest.

Second, if you're OK using the boost libraries, the vector proxy libraries may be useful.

Community
  • 1
  • 1
Will Robinson
  • 2,169
  • 1
  • 13
  • 8
0
int main()
{
    vector<int> vi;

    vi.push_back(3);
    vi.push_back(4);
    vi.push_back(5);
    vi.push_back(6);
    vi.push_back(7);

    //the end of the sequence 
    vector<int>::iterator seq_end = vi.begin() + 1;

    while (seq_end != vi.end())
    {
        cout << "......" ;
        for (vector<int>::iterator ci = vi.begin(); ci != seq_end; ++ci)
            cout << *ci << " ";
        cout << endl;

        any_function(vi.begin(), seq_end);

        ++seq_end;
    }   
}

void any_function(const vector<int>::iterator& seq_start,
                  const vector<int>::iterator& seq_end)
{
    ...
}

this code make it, analyze and repair your mistakes.

ch0kee
  • 736
  • 4
  • 12
  • Hi, what I asked was not exactly this. Suppose vector `vi` has 10 million elements. As stated above, in each iteration of the while loop, I want to pass elements from `vi` as an argument. In 1st iteration: 1 element; in 2nd, two elements (1st e 2nd), and so forth. My question was: Do I really need to create vector seg for this? If so, I need to copy elements from `vi` to `seg` in each iteration which is expensive. Or can I pass pointers to `vi` in order to avoid copying? Or another solution? – Luis Dec 30 '10 at 22:20
  • then you don't need seg, just send the vi as const reference – ch0kee Dec 31 '10 at 23:59