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 22124 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
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 the code works:
I have a stack consisting of nodes and arguments, much like a function
execution stack. In this example there are only three nodes on the
stack. For each run through the loop, a copy of the current sensation is
written to the...
|
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
Through suggestions from this news group I was able to exactly duplicate
the interface of std::vector, except for one aspect. What I need is a way
to invoke the destructor on elements of an array without de-allocating the
memory of this array. This...
|
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 even numbers.
My questions is actullay I have to put pos++ in the body of the
loop(also in if, else, statement). Can I somehow put it in
for(...;...;...).
Or can I use index instead of iterator in order to delete elements from
|
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 this purpose.
However, I am also interested in having no duplicacy in elements of
the vector. So I do not want to have any integer repeated more than
once in the list.
One very naive approach could be to compare the new integer being
added to...
| |
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 iteration. I'm
am trying to do something like:
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
|
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 hash keys and when the program is
shutting down it takes forever (10+ mins) while its deleting the objects.
The memory it uses according to task manager is varies from 500,000 k then
|
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 the function i 've written:
void table::delel( int index )
{
vector< record >::iterator rm = db.begin();
for( int i = 0; i < index; i++ )
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |