Increasing values of elements in list

I need help increasing the values of elements in mylist and assigning that new list to mylist3. I tried doing it as you would an array or vector but it did not work. Any help appreciated. Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  std::list<int> mylist (myarray, myarray + sizeof(myarray) / sizeof(int));
    cout<<"mylist contains: ";
    for (std::list<int>::iterator b = mylist.begin(); b!=mylist.end(); ++b)
    cout<<' '<< *b;

    cout<<"\n"<<endl;

std::list<int> mylist3 (10);
    for(int z = 0; z<10; z++)
    {
        mylist3[z] = mylist[z] +5;
    }
    cout<<"mylist3 contains: ";
    for(std::list<int>::iterator f = mylist3.begin(); f!=mylist3.end; ++f)
        cout<<' '<<f;
You can't use operator[] to traverse a list. You can only use iterators.

You have a typo on line 14: there's a missing pair of parentheses on the call to std::list<int>::iterator::end().
There's another typo on line 15: you forgot the asterisk in front of f.
Last edited on
Thanks for catching those typos. From what you said, helios, this is what i understood:

1
2
3
4
5
6
std::list<int> mylist3 (10);
    cout<<"mylist3 contains: ";
    for(std::list<int>::iterator f = mylist3.begin(); f!=mylist3.end(); ++f)
    f = b + 5;
    
        cout<<' '<<*f;


but it still doesnt work.
Line 4 is the only one that executes within the loop and the assignment of something (b+5) to an iterator is dubious. Perhaps you would like to change the value that the iterator points to?

Line 6 is not within the loop and thus 'f' should be invalid.
Do you mean changing f = mylist3.begin();? What else can I change it to?
No.

Your code is currently:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::list<int> mylist3 (10); // Why 10?  You were originally copying
cout<<"mylist3 contains: ";

for ( std::list<int>::iterator f = mylist3.begin(); // f points to the first node in mylist3
      f != mylist3.end();
      ++f ) // f advances to point to the next node
{
  f = b + 5; // f is set to point somewhere.  This is valid only if 'b' is an iterator to mylist3
  // did you mean:
  *f = *b + 5; // set the value of mylist3's node to 5 plus value of some node?
}
// assert: when the loop ends, the f points to mylist3.end(), an invalid location
// assert: scope of 'f' is within the loop, not here
cout << ' ' << *f; //'f' not in this scope 


There are many ways. See http://www.bobarcher.org/software/cpp_idioms.html for inspiration (and syntax).

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
std::list<int> mylist (myarray, myarray + sizeof(myarray) / sizeof(int));

std::list<int> mylist3;
for ( auto v : mylist )
{
    my_list3.push_back( v + 5 );
}


// OR
std::list<int> mylist4( mylist );
for ( auto & v : mylist4 )
{
    v += 5;
}

// OR
std::list<int> mylist5;
for ( auto f = mylist.begin(); f!=mylist.end(); ++f )
{
  mylist5.push_back( *f + 5 );
}

// and show
cout<<"mylist3 contains:";
for ( auto v : mylist3 )
{
    cout << ' ' << v;
}
// OR
cout<<"mylist3 contains: ";
std::ostream_iterator<int> out_it (std::cout, " ");
std::copy( begin(mylist3), end(mylist3), out_it );

Okay I almost have it. Now I have a problem where my output should be "mylist3 contains: 5 6 7 8 ... 14"

but instead I get an extra 10 zeros before that list of numbers, so mylist3 ends up with 20 elements?
"mylist3 contains: 0 0 0 0 0 0 0 0 0 0 5 6 7 8 ... 14"

heres my full code. The only parts that im using in this section is lines 31 - 35 and 83 - 90.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;


void printArray(int arr[], int size) {
    for ( int i = 0; i < size; i++ ) {
        cout << arr[i] << ' ';
    }
    cout << endl;
}


int main() {
    int myarray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    cout << "myarray contains: ";
    printArray(myarray, 10);

    cout<<endl;

    int myints[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::vector<int> myvector (myints, myints + sizeof(myints) / sizeof(int));
    cout<<"myvector contains: ";
    for (std::vector<int>::iterator a = myvector.begin(); a!=myvector.end(); ++a)
    cout<<' '<< *a;

    cout<<"\n"<<endl;

    int myints2 [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::list<int> mylist (myints2, myints2 + sizeof(myints2) / sizeof(int));
    cout<<"mylist contains: ";
    for (std::list<int>::iterator b = mylist.begin(); b!=mylist.end(); ++b)
        cout<<' '<< *b;

    cout<<"\n"<<endl;

    int myarray2[10] = {};
    std::copy(myarray, myarray+10, myarray2);
    cout<<"myarray2 contains: ";
    printArray(myarray2, 10);

    cout<<endl;

    std::vector<int> myvector2 (10);
    std::copy(myvector.begin(), myvector.end(), myvector2.begin());
    cout<<"myvector2 contains: ";
    for (std::vector<int>::iterator c = myvector2.begin(); c!=myvector2.end(); ++c)
        cout<<' '<<*c;

    cout<<"\n"<<endl;

    std::list<int> mylist2 (10);
    std::copy(mylist.begin(), mylist.end(), mylist2.begin());
    cout<<"mylist2 contains: ";
    for (std::list<int>::iterator d = mylist2.begin(); d!=mylist2.end(); ++d)
        cout<<' '<<*d;

    cout<<"\n"<<endl;

    int myarray3[10] = {};
    for(int i = 0; i<10; i++)
    {
        myarray3[i] = myarray[i] +2;
    }
    cout<<"myarray3 contains: ";
    printArray(myarray3, 10);

    cout<<endl;

    std::vector<int> myvector3(10);
    for(int n = 0; n<10; n++)
    {
        myvector3[n] = myvector[n]+3;
    }
    cout<<"myvector3 contains: ";
    for(std::vector<int>::iterator e = myvector3.begin(); e!=myvector3.end(); ++e)
        cout<<' '<<*e;

    cout<<"\n"<<endl;

    std::list<int> mylist3 (mylist.size());
    for (std::list<int>::iterator b = mylist.begin(); b!=mylist.end(); ++b)
    {
        mylist3.push_back( *b + 5 );
    }
    cout<<"mylist3 contains: ";
    for (std::list<int>::iterator f = mylist3.begin(); f!=mylist3.end(); ++f)
        cout<<' '<<*f;

    cout<<endl;
    
    return 0;
}
Of course it does. Line 83 creates a list (mylist3) that has mylist.size() nodes.

Loop on lines 84-87 adds another mylist.size() nodes.
Fixed it. Just replaced that mylist.size() with a 0! Thanks for all your help everyone!
In other words your final version has:
std::list<int> mylist3 ( 0 );
which is a call to the list fill constructor:
explicit list (size_type n);

However, the std::list has a default constructor:
explicit list (const allocator_type& alloc = allocator_type());
which would be called like this:
std::list<int> mylist3;


The purpose of the default constructor is to create an empty list.
The purpose of the fill constructor is to create a list with n elements (i.e. non-empty). The n==0 may or may not make the fill constructor do the same thing as the default constructor does.

It is, hopefully, just a matter of style how the code shows the intent.
Topic archived. No new replies allowed.