C++ :: Remove Duplicates Vector Of Strings But Keep First Instance - Cannot Use Algorithm
Mar 18, 2014
I have a vector of
strings.vector input;
bob
sam
bob
sammom
aardvark
money
aardvark
wanted
I need to remove the duplicates but each part of the vector corresponds to the next location. They are pairs.
ex. bob corresponds to the its definition, which is sam.
I need to keep the first instance, so keep bob and sam, but remove the second instance of bob, so remove bob and sammon. Only the first instance of the pair need to kept.
It doesn't matter if the sam and sammon don't match, all that matters is the first part of the pair.
The vector is already in alphabetical order. I can't use the algorithm library.
Code: /* generals is the first array. Max 10 elements. numGenerals is the element count of generals.
genBuff is the second array; it is to be checked/pruned. genCount is the element count of genBuff. genBuff will be a max of 171, but be pruned to no more than 10, and no more than the complement of the element count of generals. */
[Code] ....
(I do have comments in the actual source, different from above).
I have two int arrays. They hold values from 0 to 170. The first one will never be more than 10. The second will be at most 171, but will be whittled down to at most 10, usually less. 171 is worst case, most users of this particular program will probably be reasonable and not try to add all 171 (max is 10 anyway). The first array is the original array. The second array is a temporary array. Any value in the second array that is also found in the first array, is removed from the second array, since all values in the first one must be unique. After this pruning process, both arrays will collectively contain no more than 10 unique elements; the elements from the second will be added to the first.
So right now I have three nested loops. I figured with the miniscule array sizes it wouldn't be a big deal. I can think of a way to remove one or two of them, but I want to be sure that I'm still writing clean, legible, good-practice code. The first loop walks through the first array. For each element in the first array, there is a second loop to walk through the second array to check for duplicates. If a duplicate is found, the third loop walks through the second array to overwrite the duplicate while preserving the second loop's position (j).
Is this dumb? I know that the big O gets worse and worse the deeper you go with nested loops. Even though the arrays are really tiny, is this still a thing to avoid?
Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!
The goal of this is to create a list of polynomials as a <Pol> vector, where Pol is the class I developed. It "reads" the .txt, gets the strings, transforms the polynomials in the strings into class Pol objects, and it places them in the polynomial vector.I remove the strings from the text where the polynomials are, and I convert those polynomials(in string) to actual polynomials ( "Convert" in MPol.C ,bellow) , but in line 169 from Mpol.C , when I try to put "temp" ( the highest degree of the polynomial), i get a "memory corruption" error or similar. However if instead of "temp", I place "temp+1" I no longer get any errors, but i get an extra coefficient.
The goal is to merge two files of names, sort them and remove duplicates.I've managed to merge the two files into a single char array and sort them with a function so they are alphabetical.I'm having problems removing the duplicate entries from the array. Here is my code:
I want to sequentially remove one element at a time starting with the first. When the second element is removed, the first element needs to go back in. The sequence would look like
Code: // original vector, row_numbers.size()=9 row_numbers{1,2,3,4,5,6,7,8,9}; // trimmed vector, row_numbers_trim.size()=8
[Code] .......
I have been working under the assumption that the best method would be to have row_numbers remain untouched and work on a copy. For each step in the sequence, you would create row_numbers_trim as a copy of row_numbers, and then remove an element from row_numbers_trim.
Code: // position being removed int counter = 0; // copy original vector row_numbers_trim = row_numbers; // remove the first element from the copy row_numbers_trim(row_numbers_trim.begin()+counter);
All you would have to do here is to increment counter in a loop. is there a better way?
I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they walk away.
Alright I hav a program that readings from a txt file but is there a way to replace some of the words that get loaded into the vector so for example if the txt has a list of animals and i want to replace the word bird for book is their a way to do that
I'm trying to write a program that reads in from a .txt file the movie title, rating, director, actors etc. All I want to do is to just sort movie titles alphabetically and print all its attributes(director, actors). I tried using sort( movies.begin(), movies.end()) method, but it doesn't work.
I'm trying to create a database/search engine program and I'm having remove duplicate strings from a vector. I'm mostly just trying to make it so that if 2 or more movie have the same title in the database, it will remove the duplicates and just print out one copy of the movie console. I tried using the unique() command, but it doesn't seem to work.
I tried this syntax in both VS 2010 & VS 2012 Express For Desktop, and I get the same error in both compilers:
compiler error: non-aggregates cannot be initialized with initializer list
To put the code above in context, I'm going to have a .txt file with hundreds of thousands of string arrays, in initializer list format, such as:
{"string","string","string","","",""},{"string","string","string","","",""},{"string","string","string","","",""}...and so on
The first 3 strings in each array will be non-zero in length, and the last 3 strings in each array will be empty, as shown above.
I want to be able to cut and paste the arrays right into the declaration, either with:
string arrayOfArrays[0][6] = {{"string","string","string","","",""},{"string","string","string","","",""},{"string","string","string","","",""}...and so on }; or vector<vector<string> > vecOfVectors = {{"string","string","string","","",""},{"string","string","string","","",""},{"string","string","string","","",""}...and so on };
I know I can do the first, but apparently the second declaration method with vectors won't work. I would like to work with vectors, but I'm not sure about the initialization. The .txt file will be what it is, so the initialization will have to be able to work with its 'array-like initializer' format.
The code is supposed to take either an int or a string (and their respective vectors) and insert a given int or string into the vector in ascending order. My code works properly for ints, but it's having a problem with strings.
The order I get with the strings given is
penguin banana great jungle
For some reason comparing penguin to banana/great doesn't give the expected result. The template attached only includes the function and the private vectors needed for the function.
template<class T> class orderedList { public: void insert(const T& item); private: vector<T> list; int total = 0;
I have read that the Erase-remove idiom is the way to go. I have a rough understanding of how this works but am unsure whether I can implement a match-counter along with it.
For counting alone, I would use something like this:
Code: std::vector<std::string> v; // contains duplicate strings in different elements std::string term = "foo"; // search term, changing at runtime as well
unsigned int matches = 0; for( auto e : v ) { if( e == term ) {
[Code] .....
I'm not sure how (or if) I can combine the two things. That is, I don't know how to integrate a function comparing two (changing) strings into the remove_if() method. Nor do I know how to increment a counter during iteration.
The vector is extremely large, so speed is paramount. I think there are many other avenues for optimization, but decreasing the vector's size for each consecutive search could deliver a big speed boost for subsequent searches I imagine, as traversing it holds the biggest cost.
bool HashTable::insert(char const * const key, const Player& aPlayer) { //calculate the insertion position (the index of the array) size_t index = calculateIndex(key); for(int i=0; i < capacity; i++) {
[Code] ....
The inserting part works just fine, but the checking for duplicates where I compare the two values is crashing my program.
I have a program that's supposed to read in a file with comma seperated values. This file contains duplicates. The goal is to write a new file that does not contain any of the duplicates. I've successfully written the code to read in a file and create a new, identical file, but I'm failing at deleting the duplicates.
The format of each line in the file is: index,first_name,last_name,address,city,state,zip_code
This code writes the file I want (overlooking the duplicates) if I implement my equality operator as follows:
bool operator ==(const Person &a, const Person &B)/> { return false; //placeholder }
Obviously this doesn't get the job done, since it will never detect a duplicate. The problem is that whenever I try to write any meaningful code, the program writes an empty file. The idea I've been trying to implement is to compare each of the members of Person like this:
bool operator ==(const Person &a, const Person &B)/> { //if two Person objects have equivalent names, they are duplicates return ( (a.first_name == b.first_name) && (a.last_name == b.last_name) ) }
At first I thought the program was working just as before, but then deleting each line of the file as the result of an error in my code. However, I tried troubleshooting the problem by adding in
cout << a.last_name;
to parts of my code so I could see the value in certain places. Whenever I add this line or try to access a member of Person, the program writes a blank file.
I want to create a randomly ordered array of integers where there are no duplicates. Is there a way of doing this in one iteration? Or maybe even a standard function for this?
I'm looking for something like this: A2 = [3 2 1 6 7 8 4 5]