c++ and vector

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

    #1

    c++ and vector

    Hello everyone.

    I have a question about using vector.

    Given a vector<intwith a lot of integers stored inside, I would like
    to delete any elements bigger than some value, say x. Is it any
    efficient way to do that?

    Thanks.

  • Stuart Redmann

    #2
    Re: c++ and vector

    Wing wrote:
    Hello everyone.
    >
    I have a question about using vector.
    >
    Given a vector<intwith a lot of integers stored inside, I would like
    to delete any elements bigger than some value, say x. Is it any
    efficient way to do that?
    #include<vector >
    #include<iostre am>
    #include<iterat or>
    #include<functi onal>
    #include<algori thm>

    using namespace std;

    int main ()
    {
    vector<intV;
    V.push_back (2);
    V.push_back (7);
    V.push_back (5);
    V.push_back (3);
    V.push_back (9);
    V.push_back (1);

    // Print the vector.
    copy (V.begin(), V.end(), ostream_iterato r<int>(cout, " "));
    cout << endl;

    // Invoke remove_if: This method will move all elements that make
    // the predicate bind2nd (greater<int>() , 5) true to the front of
    // the vector. The return value of this invocation is the first
    // element that doesn't belong to the controlled sequence (the
    // vector's size is unchanged after the call). These elements are
    // deleted by the next call to erase (). Although this looks a bit
    // confusing, this feature can be rather handy: This way unneeded
    // re-allocations are avoided since the vector's size has
    // decreased.
    vector<int>::it erator new_end =
    remove_if(V.beg in(), V.end(),
    bind2nd (greater<int>() , 5));

    V.erase(new_end , V.end());

    // Print the vector again.
    copy(V.begin(), V.end(), ostream_iterato r<int>(cout, " "));

    return 0;
    }

    Regards,
    Stuart

    Comment

    • Stuart Redmann

      #3
      Re: c++ and vector

      Stuart Redmann wrote:
      Wing wrote:
      >
      >Hello everyone.
      >>
      >I have a question about using vector.
      >>
      >Given a vector<intwith a lot of integers stored inside, I would like
      >to delete any elements bigger than some value, say x. Is it any
      >efficient way to do that?
      >
      >
      #include<vector >
      #include<iostre am>
      #include<iterat or>
      #include<functi onal>
      #include<algori thm>
      >
      using namespace std;
      >
      int main ()
      {
      vector<intV;
      V.push_back (2);
      V.push_back (7);
      V.push_back (5);
      V.push_back (3);
      V.push_back (9);
      V.push_back (1);
      >
      // Print the vector.
      copy (V.begin(), V.end(), ostream_iterato r<int>(cout, " "));
      cout << endl;
      >
      // Invoke remove_if: This method will move all elements that make
      // the predicate bind2nd (greater<int>() , 5) true to the front of
      // the vector. The return value of this invocation is the first
      // element that doesn't belong to the controlled sequence (the
      // vector's size is unchanged after the call). These elements are
      // deleted by the next call to erase (). Although this looks a bit
      // confusing, this feature can be rather handy: This way unneeded
      // re-allocations are avoided since the vector's size has
      Sorry for the typo:
      // decreased.
      This should be replaced by
      // not decreased.
      vector<int>::it erator new_end =
      remove_if(V.beg in(), V.end(),
      bind2nd (greater<int>() , 5));
      >
      V.erase(new_end , V.end());
      >
      // Print the vector again.
      copy(V.begin(), V.end(), ostream_iterato r<int>(cout, " "));
      >
      return 0;
      }
      >
      Regards,
      Stuart

      Comment

      • Carlos Martinez

        #4
        Re: c++ and vector

        Wing wrote:
        Hello everyone.
        >
        I have a question about using vector.
        >
        Given a vector<intwith a lot of integers stored inside, I would like
        to delete any elements bigger than some value, say x. Is it any
        efficient way to do that?
        try remove_if
        >
        Thanks.
        >

        Comment

        Working...