473,756 Members | 5,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to insert elements in a 2d vector?

4 New Member
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 13608
Ganon11
3,652 Recognized Expert Specialist
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
2301
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 of a performance hit I'm taking using this technique versus using 'copy' to copy directly into a container with elements in place? Thanks, d
34
4174
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
6
4954
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 new, logical end pointer, so that the following myList::iterator end = myListObj.remove(myInt); myListObj.erase(end, myListObj.end()); is possible and removes the "invalid" items at the end of the list.
6
6558
by: Eric Lilja | last post by:
Consider the following code: #include <iostream> #include <string> #include <vector> using namespace std; int main() {
4
2793
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) : it = lower_bound(v.begin(), v.end(), greater(val)); v.insert(it,val); size_t pos = it - v.begin(); Does anyone know whether lower_bound finds the first occurrence of an
15
4916
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 that it is almost two times slower (even without using reserve() before copy) and that's because it traverses the set twice. I'm posting it as an exception to the usual (and usually correct) advise "Prefer member functions to algorithms".
5
1812
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
2537
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' and 'v_end' are valid iterators pointing to some
5
4158
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 }, { EWelcomeView, ECreateAndDestroy, IDR_MENU_OKCANCEL, RUNTIME_CLASS(CWelcomeView), NULL,0, 0 },
0
9454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10028
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9868
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6533
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5139
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2664
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.