Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inserting an element in a 2-D vector

Reply
Thread Tools

Inserting an element in a 2-D vector

 
 
Raj
Guest
Posts: n/a
 
      02-12-2010
I have a 2-D vector declared as vector<vector <uint> > solution(m,
vector<uint>n). I have initialized this vector with values. Now I want
to insert an element in the (i,j) position. I am trying to do
solution[i].insert(j, 1, value), but this gives me the error 'no
matching function for call to 'std::vector<unsigned int,
std::allocator<unsigned int> >::insert(uint&, int, int)'

How do I get this done?

Thanks,
Raj

 
Reply With Quote
 
 
 
 
tonydee
Guest
Posts: n/a
 
      02-12-2010
On Feb 12, 1:39*pm, Raj <(E-Mail Removed)> wrote:
> I have a 2-D vector declared as vector<vector <uint> > solution(m,
> vector<uint>n). I have initialized this vector with values. Now I want
> to insert an element in the (i,j) position. I am trying to do
> solution[i].insert(j, 1, value), but this gives me the error 'no
> matching function for call to 'std::vector<unsigned int,
> std::allocator<unsigned int> >::insert(uint&, int, int)'
>
> How do I get this done?
>
> Thanks,
> Raj


Given
iterator insert(iterator pos, const T& x)

Try
solution[i].insert(solution[i].begin() + j, value)

I trust you realise, in psuedo-code:
{1, 2, 4}.insert(begin() + 2, 3) == {1, 2, 3, 4}
whereas
{1, 2, 4}[2] = 3 == {1, 2, 3}.
The former is typically slower, as elements after the insertion point
need to be "moved over" to make space, and sometimes the array may
need to be resized and all elements copied. Only if this proves a
performance problem with your particular data set, you may want to
consider using a std::list<>....

Cheers,
Tony
 
Reply With Quote
 
 
 
 
Raj
Guest
Posts: n/a
 
      02-12-2010
Thanks for the answer. It works now!

Raj
On 12 fév, 01:02, tonydee <(E-Mail Removed)> wrote:
> On Feb 12, 1:39*pm, Raj <(E-Mail Removed)> wrote:
>
> > I have a 2-D vector declared as vector<vector <uint> > solution(m,
> > vector<uint>n). I have initialized this vector with values. Now I want
> > to insert an element in the (i,j) position. I am trying to do
> > solution[i].insert(j, 1, value), but this gives me the error 'no
> > matching function for call to 'std::vector<unsigned int,
> > std::allocator<unsigned int> >::insert(uint&, int, int)'

>
> > How do I get this done?

>
> > Thanks,
> > Raj

>
> Given
> * * iterator insert(iterator pos, const T& x)
>
> Try
> * * solution[i].insert(solution[i].begin() + j, value)
>
> I trust you realise, in psuedo-code:
> * * *{1, 2, 4}.insert(begin() + 2, 3) == {1, 2, 3, 4}
> whereas
> * * *{1, 2, 4}[2] = 3 == {1, 2, 3}.
> The former is typically slower, as elements after the insertion point
> need to be "moved over" to make space, and sometimes the array may
> need to be resized and all elements copied. *Only if this proves a
> performance problem with your particular data set, you may want to
> consider using a std::list<>....
>
> Cheers,
> Tony


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
can not dynamically add element to vector in going over the vector John Black C++ 4 05-28-2004 07:56 PM



Advertisments