Connecting Tech Pros Worldwide Help | Site Map

One Container question

  #1  
Old July 22nd, 2005, 12:23 PM
Pat
Guest
 
Posts: n/a
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


  #2  
Old July 22nd, 2005, 12:23 PM
Gregg
Guest
 
Posts: n/a

re: One Container question


"Pat" <Pat@Pat.com> wrote in news:40ad7b43$1_1@rain.i-cable.com:
[color=blue]
> 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[/color]

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
  #3  
Old July 22nd, 2005, 12:23 PM
Pat
Guest
 
Posts: n/a

re: One Container question


X is double number.
"Gregg" <gregg@invalid.invalid> ¦b¶l¥ó
news:Xns94F035F81D8gregginvalidinvalid@207.69.154. 203 ¤¤¼¶¼g...[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad7b43$1_1@rain.i-cable.com:
>[color=green]
> > 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[/color][/color]
larger.[color=blue][color=green]
> >
> > Thanks. Pat[/color]
>
> 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[/color]


  #4  
Old July 22nd, 2005, 12:23 PM
Gregg
Guest
 
Posts: n/a

re: One Container question


"Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
[color=blue]
> X is double number.[/color]

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
  #5  
Old July 22nd, 2005, 12:23 PM
John Harrison
Guest
 
Posts: n/a

re: One Container question



"Gregg" <gregg@invalid.invalid> wrote in message
news:Xns94F0A46AF65Egregginvalidinvalid@207.69.154 .203...[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>[color=green]
> > X is double number.[/color]
>
> Then just use a std::set<double>[/color]

And change

x.push_back(d);

to

x.insert(d);

john


  #6  
Old July 22nd, 2005, 12:23 PM
Jeff Schwab
Guest
 
Posts: n/a

re: One Container question


Gregg wrote:[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>
>[color=green]
>>X is double number.[/color]
>
>
> 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.[/color]

Do you mean std::map<double, int>?
  #7  
Old July 22nd, 2005, 12:24 PM
Paul
Guest
 
Posts: n/a

re: One Container question


Gregg wrote:
[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>
>[color=green]
>>X is double number.[/color]
>
>
> 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[/color]

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
  #8  
Old July 22nd, 2005, 12:25 PM
Gregg
Guest
 
Posts: n/a

re: One Container question


Paul <Paul@Paul.net> wrote in
news:c5f2af53d16201ad040272cdabd901c6@news.1usenet .com:
[color=blue]
> Gregg wrote:
>[color=green]
>> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>>
>>[color=darkred]
>>>X is double number.[/color]
>>
>>
>> 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[/color]
>
> 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
>[/color]

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
  #9  
Old July 22nd, 2005, 12:25 PM
Gregg
Guest
 
Posts: n/a

re: One Container question


Jeff Schwab <jeffplus@comcast.net> wrote in
news:saSdnToXmuWKejDdRVn-hw@comcast.com:
[color=blue]
> Gregg wrote:[color=green]
>> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>>
>>[color=darkred]
>>>X is double number.[/color]
>>
>>
>> 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.[/color]
>
> Do you mean std::map<double, int>?[/color]

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL container question Boltar answers 80 October 7th, 2008 10:15 AM
Tempspace only one container is being used - EEE! Visu answers 1 November 12th, 2005 11:01 AM
list container question kazio answers 9 July 19th, 2005 04:19 PM