Thread: vector remove element of element

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    vector remove element of element

    Is it possible?

    I would like to remove the first letter of a phrase.

    Something like this.

    Code:
    int main ()
    {
        vector<string> myvector;
    
        string str = "WPlease enter some integers!";
    
        myvector.push_back (str);
    
        myvector.erase(myvector[0][0]);
        cout << myvector[0] << "\n";
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course it is, but you're probably looking for something like
    myvector.at(0).erase(0);
    Can't remember if string has an indice function, though. Otherwise use iterators.

    std::vector::erase only handles what elements is inside the vector! It cannot manipulate the elements themselves stored in the vector. For that, you need to access the specific object and then call appropriate functions on it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Elysia!

    This one myvector.at(0).erase(0); erase everything after the given index apparently.

    So ill keep looking.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    This one myvector.at(0).erase(0); erase everything after the given index apparently.
    It should be:
    Code:
    myvector.at(0).erase(myvector.at(0).begin());
    Quote Originally Posted by Ducky
    So ill keep looking.
    Look at what Elysia wrote:
    Quote Originally Posted by Elysia
    Can't remember if string has an indice function, though. Otherwise use iterators.
    Next time, try before immediately replying that you'll keep looking.
    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

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Honestly, one cannot remember everything. I cannot remember exactly how the erase function(s) work. But there might be four variants:
    One that works with indices and one that works with iterators.
    Then there are those that erase a single character and those that erase a range.
    Check the documentation if unsure!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thank you Laserlight, its working perfectly!

    >>Next time, try before immediately replying that you'll keep looking.

    Im always in a hurry to reply back kinda by politeness, but if you say so im gonna take my time to study the responses before i respond in the future.
    Using Windows 10 with Code Blocks and MingW.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    It should be:
    Code:
    myvector.at(0).erase(myvector.at(0).begin());

    Look at what Elysia wrote:

    Next time, try before immediately replying that you'll keep looking.
    Personally I find creating a temporary reference more readable. It's an extra line, but as I said in my opinion more readable and maybe even faster:
    Code:
    std::string &str = myvector.at(0);
    str.erase(str.begin());
    Of course, that's just a personal (p)reference though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FREAD error
    By xphoenix in forum C Programming
    Replies: 4
    Last Post: 07-05-2010, 08:47 AM
  2. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  3. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  4. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM