vector remove element of element

This is a discussion on vector remove element of element within the C++ Programming forums, part of the General Programming Boards category; Is it possible? I would like to remove the first letter of a phrase. Something like this. Code: int main ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,764
    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
    796
    Thank you Elysia!

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

    So ill keep looking.
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,764
    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
    796
    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.
    Compiler MSVC++ 2013 with Code::Blocks.

  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, 10: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

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