Vector updation

This is a discussion on Vector updation within the C++ Programming forums, part of the General Programming Boards category; Let me put up my requirement straight away *I want The vector to be updated rather than creating a new ...

  1. #1
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Vector updation

    Let me put up my requirement straight away

    *I want The vector to be updated rather than creating a new vector

    *Will the complexity (running time) be better?

    say for eg:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
        int i,j=0;
       
          vector < vector<long long int> > g(100);
    g[1].push_back(2);
    g[2].push_back(3);
             return(0);
    }
    now all i wanted to do is to update the vector after a few steps...
    i want it to look like a new emplty vector with
    Code:
    2 associated with 1
    3 associated with 2...ie just the reverse of the previous one...i want the previous associations to be removed
    ofcourse i can do it by creating a new vector with the second associations alone....
    but i dont need that...I want the original vector to be updated with minimal complexity

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean by "associations"?

  3. #3
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    i just meant
    1 2


    1 has 2 as one of its element ...right??...i meant that
    Last edited by dpp; 05-17-2009 at 10:14 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay. So you have 100 vectors. g[1] has one element which is 2. g[2] has one element which is 3. g[0], g[3], ..., g[99] are all empty. There's no previous state that I can see to get rid of.

    If you wanted to clear g[1], you could use erase(), I guess.

  5. #5
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    ya u understood my question..

    i just want hem to be updtaed with new associations say
    now g[2] holds 1 (instead of g[1] holding 2)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to remove everything from a vector, then you can use erase(). Alternatively, you can just overwrite entries; if g[2][0] is 3, and you want it to be 1, then just assign it with g[2][0] = 1.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by tabstop View Post
    If you want to remove everything from a vector, then you can use erase().
    I think clear() would be more convenient for removing everything. However, you could you use
    Code:
    erase(vector.begin(), vector.end())

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,809
    Quote Originally Posted by linuxdude
    I think clear() would be more convenient for removing everything.
    I agree, and if one wants to actually reduce capacity in addition to just reducing size, the swap trick should be used, e.g.,
    Code:
    std::vector<long long int>().swap(g[0]);
    By the way, dpp, mind not using long long int in your examples when it is not necessary? It is not yet part of standard C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I did not know updation was a word.
    http://www.google.com/search?source=...earch&aq=f&oq=
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21