Inserting an element in a 2-D vector

Discussion in 'C++' started by Raj, Feb 12, 2010.

  1. Raj

    Raj Guest

    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.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
    Raj, Feb 12, 2010
    #1
    1. Advertising

  2. Raj

    tonydee Guest

    On Feb 12, 1:39 pm, Raj <> 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.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.insert(solution.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
    tonydee, Feb 12, 2010
    #2
    1. Advertising

  3. Raj

    Raj Guest

    Thanks for the answer. It works now!

    Raj
    On 12 fév, 01:02, tonydee <> wrote:
    > On Feb 12, 1:39 pm, Raj <> 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.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.insert(solution.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
    Raj, Feb 12, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    4
    Views:
    424
    Jeff Flinn
    May 28, 2004
  2. pmatos
    Replies:
    6
    Views:
    23,732
  3. Replies:
    8
    Views:
    1,894
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    547
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    349
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page