Sorting through a vector.
Nov 10, 2013 at 12:36am UTC
Hey forum I am trying to create a function that searches through a vector to find a name. I want to use a loop to iterate through the vector then stop and display the location of the name. I am not sure how to start I was wondering if I could get some help, thanks.
I am also trying to create a remove name function that takes out a name from input but I am also not sure why it is not working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;
char startMenu ();
void getNames (vector<string>& name);
void displayNames (vector<string>& name);
void sortNames ();
void findName (vector<string>);
void removeName ();
int main()
{
startMenu();
vector<string> name;
getNames(name);
sortNames(names);
displayNames (name);
system("pause" );
return EXIT_SUCCESS;
}
char startMenu()
{
char option = 0;
cout << "Please choose from the following list: " << endl;
cout << "Enter names: 'e', display names: 'd', sort: 's', find: 'f', or remove: 'r'." << endl;
cin >> option;
return option;
}
void getNames(vector<string>& name) {
vector<string> names;
string tmp;
while (true ) {
cout << "Enter a name (quit to stop): " ;
cin >> tmp;
if (tmp == "quit" ) break ;
names.push_back(tmp);
}
}
void displayNames (vector<string>& list)
{
if (list.size() > 0)
{
for (int n=0; n < list.size(); n++)
{
cout << list[n] << endl;
}
}
else cout << "No names stored." << endl;
}
void sortNames (vector<string>& names)
{
sort(names.begin(), names.end());
}
void findName (vector<string>& list, name)
{
cout << "Which name are you looking for?" << endl;
do
}
/*void removeName(vector<string>& list)
{
if (list.size() > 0) {
string name;
int ndx;
cout << "What name do you wise to remove?" << endl;
cin >> name;
ndx = findName (list, name);
if (ndx >= 0) {
list.erase(list.begin() + ndx);
cout << name << " has been removed " << endl;
} else cout << "The name you entered is not in the list." << endl;
}*/
Nov 10, 2013 at 2:04am UTC
remove std:: prefix if you declared
using namespace std;
, and replace
auto
with
vector<string>::iterator
if you have not enabled -std=c++0x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <vector>
#include <string>
void findName( const std::vector<std::string>& list )
{
std::string name;
std::cout << "Which name are you looking for?" << std::endl;
std::getline( std::cin, name );
int index = 0;
for ( auto itr = list.begin();
itr != list.end();
++itr,
index++)
{
if ( *itr == name )
{
std::cout << "Name found in position: " << index;
break ;
}
}
}
int main ()
{
std::vector<std::string> name( 5 );
// these are names of warcraft DOTA characters, in case your wondering :)))
name[ 0 ] = "Raijin Thunderkeg" ;
name[ 1 ] = "Nevermore" ;
name[ 2 ] = "Kael" ;
name[ 3 ] = "Mirana" ;
name[ 4 ] = "Mortred" ;
findName( name );
}
or easier, you can use find() from <algorithm>
http://www.cplusplus.com/reference/algorithm/find/
Last edited on Nov 10, 2013 at 4:13am UTC
Topic archived. No new replies allowed.