• Learning Vectors
    7 replies, posted
I am using Dev C++ and I am learning about the Standard Template Library so I decide to make a minor function to test my knowledge. I decide to try to erase the prototype and replace it with the function and still has problems. I am wondering if I made some minor mistake or what? Thanks for taking the time to read my post. code updated. This is my error log: [code] 8 C:\Users\William\Desktop\book\main.cpp variable or field `RemoveFromVector' declared void 8 C:\Users\William\Desktop\book\main.cpp missing template arguments before "list" 8 C:\Users\William\Desktop\book\main.cpp expected primary-expression before "value" 8 C:\Users\William\Desktop\book\main.cpp initializer expression list treated as compound expression C:\Users\William\Desktop\book\main.cpp In function `int main()': 24 C:\Users\William\Desktop\book\main.cpp `RemoveFromVector' cannot be used as a function 24 C:\Users\William\Desktop\book\main.cpp At global scope: 34 C:\Users\William\Desktop\book\main.cpp variable or field `RemoveFromVector' declared void 34 C:\Users\William\Desktop\book\main.cpp redefinition of `int RemoveFromVector' 8 C:\Users\William\Desktop\book\main.cpp `int RemoveFromVector' previously defined here 34 C:\Users\William\Desktop\book\main.cpp missing template arguments before "list" 34 C:\Users\William\Desktop\book\main.cpp expected primary-expression before "value" 35 C:\Users\William\Desktop\book\main.cpp expected `,' or `;' before '{' token C:\Users\William\Desktop\book\Makefile.win [Build Error] [main.o] Error 1 [/code] [code] #include <iostream> #include <vector> #include <algorithm> #inlclude <string> using namespace std; void RemoveFromVector(vector *list, string value); int main() { vector<string>list; list.push_back("test"); list.push_back("will be here afterwards"); list.push_back("test"); vector<string>::const_iterator iter; cout << "Here is the vector list: \n"; for(iter = list.begin(); iter != list.end(); ++iter) cout << (*iter) << endl; RemoveFromVector(list, "test"); cout << "After method: \n"; for(iter = list.begin(); iter != list.end(); ++iter) cout << (*iter) << endl; system("PAUSE"); return 0; } void RemoveFromVector(vector *list, string value) { vector<string>::const_iterator iter; int x = 0; for(iter = list.begin(); iter != list.end(); ++iter) { if( (*iter) == value ) list.erase(list.begin() + x); x += 1; } } [/code]
Don't use Dev C++
I don't see where you defined X for the record. Please post using [cpp] tags as it'll add line numbers.
You're creating a new vector, not passing the original one by reference. The original vector is never altered.
@Overv I forgot about that and will add to pass by address and that should allowed the original vector to be altered @Lord Nerd I added int x
Let me address your errors. [cpp]#inlclude <string>[/cpp] [i]#include[/i] is misspelled, so the compiler will ignore this header and strings cannot be used. [cpp]using namespace std;[/cpp] Not an error, but you don't want every function in the standard library in your global namespace. [cpp]void RemoveFromVector(vector list, string value);[/cpp] [i]vector[/i] is an incomplete type, you need to specify what type the list should contain as you did later on. Also note that the list is passed as a copy, so can [b]not[/b] be modified by this function. [cpp]system("PAUSE");[/cpp] This is Windows specific code and slow. A completely new process has to be started just to pause your program. [cpp]vector<string>::const_iterator iter;[/cpp] Why is the iterator not defined in the loop body? [cpp]x = 0;[/cpp] [i]x[/i] is both not needed and undefined. [cpp]if( (*iter) == value ) list.erase(list.begin() + x);[/cpp] This construction is not necessary, you can just pass the iterator to [i]erase[/i]. You have a problem though, because since vector is a linked list, your iterator will be instantly rendered invalid by calling [i]erase[/i]. My fixed code: [cpp]#include <iostream> #include <vector> #include <algorithm> #include <string> using std::vector; using std::string; using std::cout; using std::endl; void removeFromVector( vector<string>& list, string value ); int main() { vector<string> list; list.push_back( "test" ); list.push_back( "will be here afterwards" ); list.push_back( "test" ); vector<string>::const_iterator iter; cout << "Here is the vector list:\n"; for ( iter = list.begin(); iter != list.end(); iter++ ) cout << (*iter) << endl; removeFromVector( list, "test" ); cout << "After method:\n"; for ( iter = list.begin(); iter != list.end(); iter++ ) cout << (*iter) << endl; std::getchar(); return 0; } void removeFromVector( vector<string>& list, string value ) { for ( vector<string>::const_iterator iter = list.begin(); iter != list.end(); iter++ ) { if ( (*iter) == value ) { iter = list.erase( iter ); if ( iter == list.end() ) return; } } }[/cpp]
Thanks for all the help
You can additionally use [i]const string& value[/i] as well to pass the string by reference.
Sorry, you need to Log In to post a reply to this thread.