Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > One Container question

Reply
Thread Tools

One Container question

 
 
Pat
Guest
Posts: n/a
 
      05-21-2004
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


 
Reply With Quote
 
 
 
 
Gregg
Guest
Posts: n/a
 
      05-21-2004
"Pat" <> wrote in news:40ad7b43$:

> 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
 
Reply With Quote
 
 
 
 
Pat
Guest
Posts: n/a
 
      05-21-2004
X is double number.
"Gregg" <> ¦b¶l¥ó
news:Xns94F035F81D8gregginvalidinvalid@207.69.154. 203 ¤¤¼¶¼g...
> "Pat" <> wrote in news:40ad7b43$:
>
> > 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



 
Reply With Quote
 
Gregg
Guest
Posts: n/a
 
      05-21-2004
"Pat" <> wrote in news:40ad879b$:

> 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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-21-2004

"Gregg" <> wrote in message
news:Xns94F0A46AF65Egregginvalidinvalid@207.69.154 .203...
> "Pat" <> wrote in news:40ad879b$:
>
> > X is double number.

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


And change

x.push_back(d);

to

x.insert(d);

john


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-21-2004
Gregg wrote:
> "Pat" <> wrote in news:40ad879b$:
>
>
>>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>?
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      05-21-2004
Gregg wrote:

> "Pat" <> wrote in news:40ad879b$:
>
>
>>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
 
Reply With Quote
 
Gregg
Guest
Posts: n/a
 
      05-21-2004
Paul <> wrote in
news:c5f2af53d16201ad040272cdabd901c6@news.1usenet .com:

> Gregg wrote:
>
>> "Pat" <> wrote in news:40ad879b$:
>>
>>
>>>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
 
Reply With Quote
 
Gregg
Guest
Posts: n/a
 
      05-21-2004
Jeff Schwab <> wrote in
news:saSdnToXmuWKejDdRVn-:

> Gregg wrote:
>> "Pat" <> wrote in news:40ad879b$:
>>
>>
>>>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57