I encounter problems in inserting elements in a 2d vector. For example I want to insert 99 in v[2][5], how would I do it? What I have is this code (example given by Narue in http://www.daniweb.com/techtalkforums/thread70093.html)

#include <iostream>
#include <iomanip>
#include <vector>

int main()
{
  std::vector< std::vector<int> > v;

  for ( int i = 0; i < 10; i++ ) {
    v.push_back ( std::vector<int>() );

    for ( int j = 0; j < 10; j++ )
      v[i].push_back ( i + j );
  }

  std::vector< std::vector<int> >::iterator row_it = v.begin();
  std::vector< std::vector<int> >::iterator row_end = v.end();

  for ( ; row_it != row_end; ++row_it ) {
    std::vector<int>::iterator col_it = row_it->begin();
    std::vector<int>::iterator col_end = row_it->end();

    for ( ; col_it != col_end; ++col_it )
      std::cout<< std::setw ( 3 ) << *col_it;
    std::cout<<'\n';
  }

}

I tried inserting the line v.at(2).insert(col_it-5, 99); at line 27, but what I get is a segmentation fault message (I'm doing this in Linux). Can somebody tell me what I'm doing wrong. Any other suggestions on inserting elements on a 2d vector will be greatly appreaciated.

Recommended Answers

All 4 Replies

Just do:

v [2][5] = 9999 ;

to insert value at a specific location in the vector.

Vectors are almost like arrays except that they are a bit more robust along with a host of features.

Just do:

v [2][5] = 9999 ;

to insert value at a specific location in the vector.

Vectors are almost like arrays except that they are a bit more robust along with a host of features.

But doing that overwrites the current value of v[2][5]. What I need is to insert a value at v[2][5] and retain the other values, so that the the existing value of v[2][5] is now at v[2][6] and so on.

I have already tried using the following code at line 27 v.at(2).insert(col_it-5, 99); and v[2].insert(col_it-5, 99); but still I get segmentation fault messages.

I tried inserting the line v.at(2).insert(col_it-5, 99); at line 27, but what I get is a segmentation fault message (I'm doing this in Linux).

Not surprising. In the first iteration, col_it is equal to the beginning of the first column. Now you try to subtract 5 from it, you know what will happen... Boom! Segmentation fault.

If you want to insert something into a vector, you'll have to make sure that the value you're sending vector::insert() is an actual index.

Not surprising. In the first iteration, col_it is equal to the beginning of the first column. Now you try to subtract 5 from it, you know what will happen... Boom! Segmentation fault.

If you want to insert something into a vector, you'll have to make sure that the value you're sending vector::insert() is an actual index.

thanks for pointing out that I am effectively starting at the beginning and I am trying to go back.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.