vector question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pat

    vector question

    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


  • John Harrison

    #2
    Re: vector question


    "Pat" <Pat@Pat.com> wrote in message news:40ac5c1c$1 _1@rain.i-cable.com...[color=blue]
    > Hi,
    >
    > I use vector<int> to store a list of numbers. Give any number X (may not[/color]
    be[color=blue]
    > in the vector), I am looking for an efficient way to delete all X's in the
    > vector.
    >
    > Any suggestion?
    > Thanks. Pat
    >[/color]

    #include <algorithm>
    #include <vector>

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

    john


    Comment

    • Sumit Rajan

      #3
      Re: vector question


      "Pat" <Pat@Pat.com> wrote in message news:40ac5c1c$1 _1@rain.i-cable.com...[color=blue]
      > Hi,
      >
      > I use vector<int> to store a list of numbers. Give any number X (may not[/color]
      be[color=blue]
      > in the vector), I am looking for an efficient way to delete all X's in the
      > vector.
      >
      > Any suggestion?[/color]

      #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.beg in(),v.end(),
      std::ostream_it erator<int>(std ::cout, " "));
      std::cout << '\n';

      v.erase(std::re move(v.begin(), v.end(),3),v.en d()); //in vector
      v.erase(std::re move(v.begin(), v.end(),45),v.e nd()); //not in vector

      std::copy(v.beg in(),v.end(),
      std::ostream_it erator<int>(std ::cout, " "));
      std::cout << '\n';
      }

      Regards,
      Sumit.


      Comment

      • Pat

        #4
        Re: vector question

        Thanks.

        "Pat" <Pat@Pat.com> ¦b¶l¥ó news:40ac5c1c$1 _1@rain.i-cable.com ¤¤¼¶¼g...[color=blue]
        > Hi,
        >
        > I use vector<int> to store a list of numbers. Give any number X (may not[/color]
        be[color=blue]
        > in the vector), I am looking for an efficient way to delete all X's in the
        > vector.
        >
        > Any suggestion?
        > Thanks. Pat
        >
        >[/color]


        Comment

        Working...