vector string order

My program is almost done, the only thing i need to do is order the names alphabetically as they are being printed out. I made this function...

1
2
3
4
5
6
7
8
9
 
void check(vector<string>&name,vector<string>&name2) {
	if (name[0] > name2[0]) {
		cout << name << endl;
	}
	if (name[0] < name2[0]) {
		cout << name2 << endl;
	}
}

but im not sure if i can make it work since it needs two strings, unless i can compare the strings that have been printed out.

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
 #include <iostream>
#include <cctype>
#include <string>
#include <vector>

using namespace std;

char choice();


int main() {
	string bus;
	
	vector<string>name;
	while (choice()=='Y') {
		
		
		cout << "Please enter name of your business." << endl;
		cin >> bus ;
		name.push_back(bus);
		if (name.size() > 1) {
			cout << "The name of your buisnesses are: " << endl;
		}
		else {
			cout << "The name of your business is: ";
		}
		for (unsigned int i = 0; i < name.size(); i++) {
		
			cout << '\n'<<name[i] << endl;
		}

	}


	





		
	

	


	
	
	system("pause");
	return 0;
}



char choice() {
	cout << "\nWould you like to use the program?" << endl;
	char x;
	cin >> x;
	cin.ignore(INT_MAX, '\n');
	return toupper(x);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    vector<string> name {"harry", "tom", "apple", "zoology", "basket"};
    sort(name.begin(), name.end());
    for(auto& elem : name)
    {
        cout<<elem<<"**";
    }
}
//Output:
apple**basket**harry**tom**zoology**
Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.
OP: you can even use set (unique strings only) or multi-set (non-unique strings as well) that will sort the strings automatically or by any user-defined criterion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <set>
#include <string>
using namespace std;

int main()
{
    multiset<string> name {"harry", "tom", "apple", "zoology", "basket", "tom"};
    cout<<"Enter another name: \n";
    string new_name{};
    cin>>new_name;
    name.insert(new_name);
    for(auto& elem : name)
    {
        cout<<elem<<"**";
    }
}

Thanks, but I cant use anything outside the libraries that i have already
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    vector<string> name {"harry", "tom", "apple", "zoology", "basket"};
    for(size_t i = 0; i < name.size(); i++)
    {
        for(size_t j = i+1; j < name.size(); j++)
        {
            string temp{};
            if(name[j] < name[i])
            {
               temp = name[i];
                name[i] = name[j];
                name[j] = temp;
            }
        }
    }
    for(auto& elem : name)
    {
        cout<<elem<<"**";
    }
}
OP: one thing to bear in mind is that if you have any upper case strings in the vector then without using any other libraries (like algorithm, locale etc) I'm not sure you can get the words beginning with upper and lower case versions of the same letter next to each other with the program as it stands now:

for e.g:
1
2
 vector<string> name {"harry", "tom", "apple", "Tank", "zoology", "basket"};
Output:Tank**apple**basket**harry**tom**zoology**

Thanks a lot that's makes a lot of sense.
You can template your code to reuse it and possibly earn some extra credits ;)

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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

template <typename T>
const vector<T>& ascending(vector<T>& v)
{
    for(size_t i = 0; i < v.size(); i++)
    {
        for(size_t j = i+1; j < v.size(); j++)
        {
            T temp{};
            if(v[j] < v[i])
            {
               temp = v[i];
                v[i] = v[j];
                v[j] = temp;
            }
        }
    }
    return v;
}
int main()
{
    vector<string> name {"harry", "tom", "apple", "zoology", "basket"};
    ascending(name);
    for(auto& elem : name)
    {
        cout<<elem<<"**";
    }
}
Last edited on
//Aide pour vecteur

#include <iostream>
#include <vector>
#include <string>

int main(){
std::vector<int>::iterator it;
vector<Question> jeu;

cin.ignore();
for(int ctr = 0;ctr<jeu.size();ctr++){ //Boucle pour poser toutes les questions, 1 à la fois.

fusion = jeu[ctr].getMot1()+" - "+jeu[ctr].getMot2()+" - "+jeu[ctr].getMot3()+" ";
cout<<"Question #"<<(ctr+1) << " "<<fusion<<endl;
cout<<"reponse ";

getline(cin,reponse);



if(reponse == jeu[ctr].getReponse()){

cout<<"Bonne reponse"<<endl;
bonneReponse++;
}

//Insérer a la position demander
jeu.insert(jeu.begin()+(position),Question(reponse,mot1,mot2,mot3));

//Insérer a la fin
jeu.push_back(Question(reponse,mot1,mot2,mot3));

//Efface a la position deamnder
jeu.erase(jeu.begin()+position);

y
Last edited on
Topic archived. No new replies allowed.