One Container question

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

  1. Pat

    Pat Guest

    When inserting an number, say X, into a vector, if X already is in the
    vector, then do nothing ( because I do want a repeated X occurs in the
    vector). Otherwise, push X into the container.

    Any efficient way to do this? Because my vector container is very larger.

    Thanks. Pat
     
    Pat, May 21, 2004
    #1
    1. Advertising

  2. Pat

    Gregg Guest

    "Pat" <> wrote in news:40ad7b43$-cable.com:

    > When inserting an number, say X, into a vector, if X already is in the
    > vector, then do nothing ( because I do want a repeated X occurs in the
    > vector). Otherwise, push X into the container.
    >
    > Any efficient way to do this? Because my vector container is very larger.
    >
    > Thanks. Pat


    If the type of X has (or can have) a relational operator defined on it,
    then you ou can use a std::set<> in parallel with the vector to check for
    existence in the set before adding it to the vector. Otherwise, you will
    have to do a linear search using std::find. Is it possible you don't need
    the vector at all and that what you really want is a set?

    Gregg
     
    Gregg, May 21, 2004
    #2
    1. Advertising

  3. Pat

    Pat Guest

    X is double number.
    "Gregg" <> ¦b¶l¥ó
    news:Xns94F035F81D8gregginvalidinvalid@207.69.154.203 ¤¤¼¶¼g...
    > "Pat" <> wrote in news:40ad7b43$-cable.com:
    >
    > > When inserting an number, say X, into a vector, if X already is in the
    > > vector, then do nothing ( because I do want a repeated X occurs in the
    > > vector). Otherwise, push X into the container.
    > >
    > > Any efficient way to do this? Because my vector container is very

    larger.
    > >
    > > Thanks. Pat

    >
    > If the type of X has (or can have) a relational operator defined on it,
    > then you ou can use a std::set<> in parallel with the vector to check for
    > existence in the set before adding it to the vector. Otherwise, you will
    > have to do a linear search using std::find. Is it possible you don't need
    > the vector at all and that what you really want is a set?
    >
    > Gregg
     
    Pat, May 21, 2004
    #3
  4. Pat

    Gregg Guest

    "Pat" <> wrote in news:40ad879b$-cable.com:

    > X is double number.


    Then just use a std::set<double> to keep track of what you've added to the
    vector. You might also consider using std::map<int, double> and not use the
    vector at all. The suitablility of this depends on how often you will be
    looking up entries versus adding them. Perhaps you don't need to look up by
    index at all, in which case just use a set and do away with the vector.

    Gregg
     
    Gregg, May 21, 2004
    #4
  5. "Gregg" <> wrote in message
    news:Xns94F0A46AF65Egregginvalidinvalid@207.69.154.203...
    > "Pat" <> wrote in news:40ad879b$-cable.com:
    >
    > > X is double number.

    >
    > Then just use a std::set<double>


    And change

    x.push_back(d);

    to

    x.insert(d);

    john
     
    John Harrison, May 21, 2004
    #5
  6. Pat

    Jeff Schwab Guest

    Gregg wrote:
    > "Pat" <> wrote in news:40ad879b$-cable.com:
    >
    >
    >>X is double number.

    >
    >
    > Then just use a std::set<double> to keep track of what you've added to the
    > vector. You might also consider using std::map<int, double> and not use the
    > vector at all.


    Do you mean std::map<double, int>?
     
    Jeff Schwab, May 21, 2004
    #6
  7. Pat

    Paul Guest

    Gregg wrote:

    > "Pat" <> wrote in news:40ad879b$-cable.com:
    >
    >
    >>X is double number.

    >
    >
    > Then just use a std::set<double> to keep track of what you've added to the
    > vector. You might also consider using std::map<int, double> and not use the
    > vector at all. The suitablility of this depends on how often you will be
    > looking up entries versus adding them. Perhaps you don't need to look up by
    > index at all, in which case just use a set and do away with the vector.
    >
    > Gregg


    The problem with std::set<double> is that double values rarely are
    equal. Tests for equality (as std::set::find() does) will yield
    inconsistent results.

    Paul
     
    Paul, May 21, 2004
    #7
  8. Pat

    Gregg Guest

    Paul <> wrote in
    news:c5f2af53d16201ad040272cdabd901c6@news.1usenet.com:

    > Gregg wrote:
    >
    >> "Pat" <> wrote in news:40ad879b$-cable.com:
    >>
    >>
    >>>X is double number.

    >>
    >>
    >> Then just use a std::set<double> to keep track of what you've added
    >> to the vector. You might also consider using std::map<int, double>
    >> and not use the vector at all. The suitablility of this depends on
    >> how often you will be looking up entries versus adding them. Perhaps
    >> you don't need to look up by index at all, in which case just use a
    >> set and do away with the vector.
    >>
    >> Gregg

    >
    > The problem with std::set<double> is that double values rarely are
    > equal. Tests for equality (as std::set::find() does) will yield
    > inconsistent results.
    >
    > Paul
    >


    I agree in general, but checking for equality was the OP's requirement.
    Depending on how the doubles are being generated (e.g., are they being
    read from a file that contains dollars and cents amounts), this may or
    may not be wise.

    Gregg
     
    Gregg, May 22, 2004
    #8
  9. Pat

    Gregg Guest

    Jeff Schwab <> wrote in
    news::

    > Gregg wrote:
    >> "Pat" <> wrote in news:40ad879b$-cable.com:
    >>
    >>
    >>>X is double number.

    >>
    >>
    >> Then just use a std::set<double> to keep track of what you've added
    >> to the vector. You might also consider using std::map<int, double>
    >> and not use the vector at all.

    >
    > Do you mean std::map<double, int>?


    No, I meant a map that would enable an int to be supplied as a key to look
    up a double, so it could serve as a possible substitute for a vector
    <double>.

    Gregg
     
    Gregg, May 22, 2004
    #9
    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. Vivi Orunitia
    Replies:
    11
    Views:
    4,502
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    536
    Maitre Bart
    Feb 11, 2004
  3. Replies:
    3
    Views:
    387
    teddyj
    Sep 28, 2006
  4. Replies:
    4
    Views:
    815
    Daniel T.
    Feb 16, 2006
  5. Replies:
    5
    Views:
    563
    Thomas J. Gritzan
    Oct 6, 2006
Loading...

Share This Page