vector question

Discussion in 'C++' started by Pat, May 20, 2004.

  1. Pat

    Pat Guest

    Hi,

    I use vector<int> to store a list of numbers. Give any number X (may not be
    in the vector), I am looking for an efficient way to delete all X's in the
    vector.

    Any suggestion?
    Thanks. Pat
     
    Pat, May 20, 2004
    #1
    1. Advertising

  2. "Pat" <> wrote in message news:40ac5c1c$-cable.com...
    > Hi,
    >
    > I use vector<int> to store a list of numbers. Give any number X (may not

    be
    > in the vector), I am looking for an efficient way to delete all X's in the
    > vector.
    >
    > Any suggestion?
    > Thanks. Pat
    >


    #include <algorithm>
    #include <vector>

    vec.erase(std::remove(vec.begin(), vec.end(), val), vec.end());

    john
     
    John Harrison, May 20, 2004
    #2
    1. Advertising

  3. Pat

    Sumit Rajan Guest

    "Pat" <> wrote in message news:40ac5c1c$-cable.com...
    > Hi,
    >
    > I use vector<int> to store a list of numbers. Give any number X (may not

    be
    > in the vector), I am looking for an efficient way to delete all X's in the
    > vector.
    >
    > Any suggestion?


    #include <iostream>
    #include <vector>
    #include <algorithm>

    int main()
    {


    std::vector<int> v;
    for (int i=0; i<10;i++)
    v.push_back(i);
    v.push_back(3);
    v.push_back(4);

    std::copy(v.begin(),v.end(),
    std::eek:stream_iterator<int>(std::cout, " "));
    std::cout << '\n';

    v.erase(std::remove(v.begin(),v.end(),3),v.end()); //in vector
    v.erase(std::remove(v.begin(),v.end(),45),v.end()); //not in vector

    std::copy(v.begin(),v.end(),
    std::eek:stream_iterator<int>(std::cout, " "));
    std::cout << '\n';
    }

    Regards,
    Sumit.
     
    Sumit Rajan, May 20, 2004
    #3
  4. Pat

    Pat Guest

    Thanks.

    "Pat" <> ¦b¶l¥ó news:40ac5c1c$-cable.com ¤¤¼¶¼g...
    > Hi,
    >
    > I use vector<int> to store a list of numbers. Give any number X (may not

    be
    > in the vector), I am looking for an efficient way to delete all X's in the
    > vector.
    >
    > Any suggestion?
    > Thanks. Pat
    >
    >
     
    Pat, May 21, 2004
    #4
    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. BCC
    Replies:
    12
    Views:
    838
    Clark Cox
    Feb 6, 2004
  2. pmatos
    Replies:
    6
    Views:
    23,937
  3. Replies:
    8
    Views:
    1,960
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    584
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    374
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page