473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to insert elements in a 2d vector?

4
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.   #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5.  
  6. int main()
  7. {
  8.   std::vector< std::vector<int> > v;
  9.  
  10.   for ( int i = 0; i < 10; i++ ) {
  11.     v.push_back ( std::vector<int>() );
  12.  
  13.     for ( int j = 0; j < 10; j++ )
  14.       v[i].push_back ( i + j );
  15.   }
  16.  
  17.   std::vector< std::vector<int> >::iterator row_it = v.begin();
  18.   std::vector< std::vector<int> >::iterator row_end = v.end();
  19.  
  20.   for ( ; row_it != row_end; ++row_it ) {
  21.     std::vector<int>::iterator col_it = row_it->begin();
  22.     std::vector<int>::iterator col_end = row_it->end();
  23.  
  24.     for ( ; col_it != col_end; ++col_it )
  25.       std::cout<< std::setw ( 3 ) << *col_it;
  26.     std::cout<<'\n';
  27.   }
  28.  
  29. //line 27
  30.  
  31. }
  32.  
  33.  
  34.  
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). I also tried v[2].insert(col_it-5, 99) and the same thing happened. What I need is to insert not to overwrite the contents of v[2][5].
Can somebody tell me what I'm doing wrong. Any other suggestions on inserting elements on a 2d vector will be greatly appreciated.
Feb 18 '07 #1
1 13528
Ganon11
3,652 Expert 2GB
Are you sure you should be writing col_it - 5? Also, where in the program are you trying to insert this? You may be attempting to do this at a point when col_it is not pointing to an element in the 3rd row (index 2) of the vector.
Feb 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a...
6
by: Eric Lilja | last post by:
Consider the following code: #include <iostream> #include <string> #include <vector> using namespace std; int main() {
4
by: Thomas Kowalski | last post by:
Hi, I have an sorted vector<doublev and want to insert a new double val. What I need is the position the val have been inserted at in the vector. My basic idea is something like this (pseudo-code)...
15
by: kostas | last post by:
Hi, Trying to dump a set<int(s) in a vector (v) my guess was that v.insert(v.end(),s.begin(),s.end()) would be at least as efficient as copy(s.begin(), s.end(), back_inserter(v)) It turn out...
5
by: pwalker | last post by:
Hi everyone, I am trying to get my head around iterators used on vectors. Let's take the code below: ------------- std::vector<intv1; std::vector<intv2; v1.push_back( 1 );
1
by: subramanian100in | last post by:
Suppose I have vector<intvi; deque<intdi; list<intli; Suppose all of these containers have some elements. Suppose 'v_iter' is an iterator pointing to some element in 'vi'. Suppose 'v_beg'...
5
by: John Doe | last post by:
Hi, I have a static array of struct defined like this : CViewMgr::ViewInfo g_ViewInfo = { { EMainView, ECreateOnce, IDR_MAINFRAME, RUNTIME_CLASS(CMainView), NULL,0, 0 }, {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.