Hi,
vector<string> str;
I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.
Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".
How can I do that??
Please help
Thanks!!
10 22088 sicarie 4,677
Recognized Expert Moderator Specialist
Hi,
vector<string> str;
I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.
Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".
How can I do that??
Please help
Thanks!!
I would recommend using a second vector and a for loop to iterate through the elements ( vector iterator). Then you can compare the current vector against all the elements of the second vector. Does that make sense, or do I need to get into a bit more detail?
Hi,
vector<string> str;
I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.
Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".
How can I do that??
Please help
Thanks!!
Do you really need a vector ??
If you have to eliminate duplicates then using a set is most useful as every element is added only once.
/ Sid
Hi Sicarie,
Can you please give me more details on what you have said, as I am not able to exactly grasp what you meant.
I have a large set of code where I have been using vectors, so now I will not be able to switch from vectors to sets.
What is the possible way to achieve this using vectors??
Thanks!!
sicarie 4,677
Recognized Expert Moderator Specialist
Hi,
vector<string> str;
I have the vector filled with strings "a", "b", "c", "b", "c", "d", "a".
i.e
str[0] = a; str[1] = b; str[2] = c;.....and so on.
Now I would like to delete the duplicate elements in the above vector such that the vector only contains distinct elemets "a", "b", "c" and "d".
How can I do that??
Please help
Thanks!!
my algorithm would probably look something like this -
vector str
-
vector str_noDuplicates
-
str Iterator
-
element_str
-
element_strNoDuplicates
-
-
for each element in str
-
element_str = next element
-
for each element in str_noDuplicates
-
element_strNoDuplicates = next element
-
if element_str == element_strNoDuplicates
-
set duplicateFlag to true
-
end if
-
end for
-
if duplicateFlag == false
-
add element_strNoDuplicates to str_noDuplicates
-
else
-
do nothing
-
end for
-
Does that help?
RRick 463
Recognized Expert Contributor
This is where the builtin STL functions come in handy.
First you sort the array and then use unique to remove adjacent duplicates. -
vector<int> ivec;
-
-
..... add all the entries you like ......
-
-
sort( ivec.begin(), ivec.end());
-
unique( ivec.begin), ivec.end());
-
I'm not going to say this is efficient because removing entries in the middle of a vector can be expensive.
sicarie 4,677
Recognized Expert Moderator Specialist
This is where the builtin STL functions come in handy.
First you sort the array and then use unique to remove adjacent duplicates. -
vector<int> ivec;
-
-
..... add all the entries you like ......
-
-
sort( ivec.begin(), ivec.end());
-
unique( ivec.begin), ivec.end());
-
I'm not going to say this is efficient because removing entries in the middle of a vector can be expensive.
Psh, well, if you want to do it the easy way.
@RRick - you ever going to answer my PM? ;)
RRick 463
Recognized Expert Contributor
Ironically, I have never used these functions, but I have a very good book.
A lot of us get stuck at the container level of STL and rarely venture beyond the iterators and containers. I consider the STL algorithms/functions to be the next step in my STL coding.
There is no unique function available for vectors in STL.
It is for list
hsn 237
New Member
if you want to do what you want to do by using vectors it will take you a while to do it. also it will be slow way.
by using set you only enter the values to the set and it will have only unique values.
for example
if you entered 'a' 'b' 'a' 'c'
in the set it will be 'a' 'b' 'c'. without calling any functions.
GOOD LUCK
RRick 463
Recognized Expert Contributor
Gpkaur, there is an STL unique function, but it's found in the <algorithm> header file, not in each object. List is "special" because it does have its own unique function.
The concept of the algorithm functions is that they work for all containers. Separate containers add their own algorithms when a specific need (usually speed) is required.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Martin Magnusson |
last post by:
I'm getting segmentation faults when trying to fix a memory leak in my
program. The problem is related to lists of pointers which get passed
around between objects.
Here is a description of how...
|
by: Peter Olcott |
last post by:
I have just built a class that provides the most useful subset of std::vector
functionality for use by compilers that lack template capability.
http://home.att.net/~olcott/std_vect.html
...
|
by: david wolf |
last post by:
I want to delete all even numbers in a vector, I am not sure if there's
any better way to do it. Following program is how I did it.
Look at the part of code beginning from
comments: //delete all...
|
by: Jon Slaughter |
last post by:
I'm having a problem allocating some elements of a vector then deleting
them.
Basicaly I have something like this:
class base
{
private:
std::vector<object> V;
|
by: Amit |
last post by:
Hi,
I have a list of integers. At each iteration, I remove some element
from it and then insert new elements in it. The order of elements is
not important. So I guess I could use a vector also for...
| |
by: cayblood |
last post by:
I want to iterate through a vector and erase elements that meet a
certain criteria. I know there is an algorithmic way of doing this,
but first I would like to know how to do it with normal...
|
by: Whybother |
last post by:
I have this
typedef map<__int64, int> Results_Map;
__int64 is defined as 8 bytes ranging from -9,223,372,036.854,775,808 to
9,223,372,036.854,775,807
I have loaded it with approx 34 million...
|
by: streamkid |
last post by:
i have a class table, which has a vector of records(-db). i 'm trying
to remove an element,
but it doesn't seem to work..
i read this http://www.cppreference.com/cppvector/erase.html] and
that's...
|
by: subramanian100in |
last post by:
Suppose
class Base
{
public:
virtual ~Test() { ... }
// ...
};
class Derived : public Base
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |