Vector crashing

Why is my vector program kept on crashing
There's the question:
5. Use C++ Standard Template Library <vector> to do the followings:
(a) Declare a vector named v1 of type int.
(b) Use function push_back() to store value 1, 2, 3 and 4 into v1.
(c) Print out the size of v1.
(d) Use a for loop to print out each element value of v1.
(e) Assign a new value 5 to element 1.
(f) Remove the last element.
(g) Print out all elements of v1 using an iterator named it to access each element of v1.
(h) Insert new element value 10 at position 0 (the first element).
(i) Use erase() to remove element at position 2.
(j) Use a for loop to print out each element value of v1 again.
(k) Remove all the elements in the vector v1.
(l) Check if the vector v1 is empty.

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
  #include <iostream>
#include<vector>
using namespace std;

int main()
{
    vector <int> vl;
    for(int i=1;i<=4;i++)
    {
        vl.push_back(i);
    }
    cout<<"Vector size= "<<vl.size()<<endl;
    for(int i=0;i<vl.size();i++)
    {
        cout<<"Value of vl["<<i<<"]: "<<vl[i]<<endl;
    }
    cout<<endl;
    vl.assign(1,5);
     for(int i=0;i<vl.size();i++)
    {
        cout<<"Value of vl["<<i<<"]: "<<vl[i]<<endl;
    }
    cout<<endl;
    vl.pop_back();
    vector<int>::iterator it;
   for(it=vl.begin();it!=vl.end();++it)
   {
       cout<<"Value of vector: "<<*it<<" ";
   }
   cout<<endl;
    vl.insert(vl.begin(),10);
    vl.erase(vl.begin()+2);
         for(int i=0;i<vl.size();i++)
    {
        cout<<"Value of vl["<<i<<"]: "<<vl[i]<<endl;
    }
    vl.clear();
    if(vl.empty()==true)
    {
        cout<<"\nThe vector vl is empty."<<endl;
    }
    else if(vl.empty()==false)
    {
        cout<<"\nThe vector vl isn't empty."<<endl;
    }
    return 0;
}
Last edited on
and compiler shows "comparison between signed and unsigned integers"
1
2
3
4
     for(int i=0;i<vl.size();i++)
    {
        cout<<"Value of vl["<<i<<"]: "<<vl[i]<<endl;
    }
What type is vl.size() ?

Also, when programs crash, run them in the debugger.
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
$ g++ -g -Wall -Wextra baz.cpp
baz.cpp: In function ‘int main()’:
baz.cpp:13:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<vl.size();i++)
                  ^
baz.cpp:19:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      for(int i=0;i<vl.size();i++)
                   ^
baz.cpp:33:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
          for(int i=0;i<vl.size();i++)
                       ^
$ gdb -q ./a.out 
Reading symbols from ./a.out...done.
(gdb) run
Starting program: ./a.out 
Vector size= 4
Value of vl[0]: 1
Value of vl[1]: 2
Value of vl[2]: 3
Value of vl[3]: 4

Value of vl[0]: 5



Program received signal SIGSEGV, Segmentation fault.
__memmove_ssse3_back () at ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:1547
1547	../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S: No such file or directory.
(gdb) bt
<< snipped for brevity >>
#6  0x00000000004015d3 in std::vector<int, std::allocator<int> >::erase (this=0x7fffffffde30, \
     __position=4) at /usr/include/c++/5/bits/stl_vector.h:1150
#7  0x0000000000400f93 in main () at baz.cpp:32
(gdb) 

Right, the reason for the crash is that it didn't like the erase() call at line 32 in your main() function.

> vl.erase(vl.begin()+2);
You're assuming that simple addition results in a valid iterator. Apparently, this is not so.

http://www.cplusplus.com/reference/iterator/advance/
so how should I modify the codes?
and vl.size() is the function to find the size of vector? i guess
Last edited on
vl.assign(1,5) resets the vector's contents to one element (value 5).
So v1.begin() + 2 will be past the end.

So you just need to understand what assign() does, and use it in a more sensible way.
Last edited on
> so how should I modify the codes?

Change line 32 to:
1
2
3
// if the vector contains at least three items, 
// delete the third item (the item at position 2)
if( vl.size() > 2 ) vl.erase( vl.begin()+2 );

Note: this will do nothing since the vector is too small (size == 1).
nope, not working.
it always stop between the assign() and popback() fuction
This appears to work as expected:
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
#include <iostream>
#include<vector>
using namespace std;

int main()
{
    vector <int> vl;
    for(int i=1;i<=4;i++)
    {
        vl.push_back(i);
    }
    cout<<"Vector size= "<<vl.size()<<endl; // size == 4
    for(int i=0;i<vl.size();i++)
    {
        cout<<"Value of vl["<<i<<"]: "<<vl[i]<<endl;
    }
    cout<<endl;
    vl.assign(1,5);
    std::cout << "after vl.assign(1,5); size == " << vl.size() << '\n' ; // size == 1

     for(int i=0;i<vl.size();i++)
    {
        cout<<"Value of vl["<<i<<"]: "<<vl[i]<<endl;
    }
    cout<<endl;
    vl.pop_back();
    std::cout << "after vl.pop_back(); size == " << vl.size() << '\n' ; // size == 0
    vector<int>::iterator it;
   for(it=vl.begin();it!=vl.end();++it)
   {
       cout<<"Value of vector: "<<*it<<" ";
   }
   cout<<endl;
    vl.insert(vl.begin(),10);
    std::cout << "after vl.insert(vl.begin(),10); size == " << vl.size() << '\n' ; // size == 1

    // if the vector contains at least three items,
    // delete the third item (the item at position 2)
    if( vl.size() > 2 ) vl.erase( vl.begin()+2 );

    for(int i=0;i<vl.size();i++)
    {
        cout<<"Value of vl["<<i<<"]: "<<vl[i]<<endl;
    }
    vl.clear();
    if(vl.empty()==true)
    {
        cout<<"\nThe vector vl is empty."<<endl;
    }
    else if(vl.empty()==false)
    {
        cout<<"\nThe vector vl isn't empty."<<endl;
    }
    
    std::cout << "reached end of main\n" ;
    return 0;
}

http://coliru.stacked-crooked.com/a/2a2501063de95238
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
#include <iostream>
#include<vector>

int main()
{
    // (a) Declare a vector named v1 of type int.
    std::vector<int> vl ;

    // (b) Use function push_back() to store value 1, 2, 3 and 4 into v1.
    for( int i = 1 ; i < 5 ; ++i ) vl.push_back(i) ;

    // (c) Print out the size of v1.
    std::cout << "(c): size == " << vl.size() << '\n' ;

    // (d) Use a for loop to print out each element value of v1.
    std::cout << "(d): [ " ;
    // range based for loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( int v : vl ) std::cout << v << ' ' ;
    std::cout << "]\n" ;

    // (e) Assign a new value 5 to element 1.
    vl[1] = 5 ;

    // (f) Remove the last element.
    vl.pop_back() ;

    // (g) Print out all elements of v1 using an iterator named it to access each element of v1.
    std::cout << "(g): [ " ;
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    for( auto it = vl.begin() ; it != vl.end() ; ++it ) std::cout << *it << ' ' ;
    std::cout << "]\n" ;

    // (h) Insert new element value 10 at position 0 (the first element).
    vl.insert( vl.begin(), 10 ) ;

    // (i) Use erase() to remove element at position 2.
    vl.erase( vl.begin() + 2 ) ;

    // (j) Use a for loop to print out each element value of v1 again.
    std::cout << "(j): [ " ;
    for( int v : vl ) std::cout << v << ' ' ;
    std::cout << "]\n" ;

    // (k) Remove all the elements in the vector v1.
    vl.clear() ;

    // (l) Check if the vector v1 is empty.
    std::cout << "(l): empty? " << std::boolalpha << vl.empty() << '\n' ;
}

http://coliru.stacked-crooked.com/a/afda88aa55540b9b
but I think are the answers right?
(a) Declare a vector named v1 of type int.
(b) Use function push_back() to store value 1, 2, 3 and 4 into v1.
(c) Print out the size of v1.
(d) Use a for loop to print out each element value of v1.
(e) Assign a new value 5 to element 1.
(f) Remove the last element.
(g) Print out all elements of v1 using an iterator named it to access each element of v1.
(h) Insert new element value 10 at position 0 (the first element).
(i) Use erase() to remove element at position 2.
(j) Use a for loop to print out each element value of v1 again.
(k) Remove all the elements in the vector v1.
(l) Check if the vector v1 is empty.
ohh!!! finally done. Thanks guys
so actually we couldn't use assign() function to assign the value 5 to element 1?
This is my final code. It seems to work well
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
#include <iostream>
#include<vector>
using namespace std;

int main()
{
    vector <int> v1;
    for(int i=1;i<=4;i++)
    {
        v1.push_back(i);
    }
    cout<<"Vector size= "<<v1.size()<<endl;
    for(int i=0;i<v1.size();i++)
    {
        cout<<"Value of v1["<<i<<"]: "<<v1[i]<<endl;
    }
    cout<<endl;
    v1[1]=5;
    v1.pop_back();
    vector<int>::iterator it;
 for(it=v1.begin();it!=v1.end();++it)
   {
       cout<<"Value of vector: "<<*it<<endl;
   }

    v1.insert(v1.begin(),10);
    cout << "After v1.insert(v1.begin(),10); size == "<<v1.size()<<endl;
     // if the vector contains at least three items,
    // delete the third item (the item at position 2)
    v1.erase( v1.begin()+2 );
    cout << "After v1.erase( v1.begin()+2 ); size == "<<v1.size()<<endl;
         for(int i=0;i<v1.size();i++)
    {
        cout<<"Value of v1["<<i<<"]: "<<v1[i]<<endl;
    }
    v1.clear();
    if(v1.empty()==true)
    {
        cout<<"\nThe vector v1 is empty."<<endl;
    }
    else if(v1.empty()==false)
    {
        cout<<"\nThe vector v1 isn't empty."<<endl;
    }
    return 0;
}
Last edited on
Jochebed111 wrote:
so actually we couldn't use assign() function to assign the value 5 to element 1?


No. That's not what the .assign() member function does.


Your "assigned" task was to put the value 5 into the [1] element as @JLBorges' code does:
1
2
    // (e) Assign a new value 5 to element 1.
    vl[1] = 5 ;

Here, "assignment" is done with the "=" operator. That was the intention of the exercise (deduced from the context).


Sometimes, English words are used in rather different senses. In this instance it has caused confusion.
Last edited on
> so actually we couldn't use assign() function to assign the value 5 to element 1?

The member function assign() assigns a new value to the vector
(it replaces the entire current contents of the vector with a new sequence).

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
#include <iostream>
#include <vector>

int main()
{
    // initialise the vector with the values in the list
    std::vector<int> vec { 1, 2, 3, 4, 5 };
    
    // lambda: http://www.stroustrup.com/C++11FAQ.html#lambda
    const auto print_vec = [&vec] // print the contents of the vector
    { std::cout << "[ " ; for( int v : vec ) std::cout << v << ' ' ; std::cout << "]\n" ; };

    print_vec() ; // [ 1 2 3 4 5 ]

    // replace the contents of the vector with the values in the list
    vec.assign( { 70, 71, 72, 73, 74 } ) ;
    print_vec() ; // [ 70 71 72 73 74 ]

    // replace the contents of the vector with 7 copies of the value 99
    vec.assign( 7, 99 ) ;
    print_vec() ; // [ 99 99 99 99 99 99 99 ]

    // replace the contents of the vector with the values in the list
    vec = { 67, 56, 45, 32, 21 } ;
    print_vec() ; // [ 67 56 45 32 21 ]
}

http://coliru.stacked-crooked.com/a/797c2e5e08d597f3
Topic archived. No new replies allowed.