Bubble sort more than 2 vectors? help!

TLDR;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	
	for (int j = 0; j<months.size()-1;++j)
		(int k = 0; k<years.size()-1;++k)		
		(int l = 0; l<appointments.size()-1;++l)		 {
		temp1 = months[j];
		temp2 = years[k];
		temp3 = appointments[l];	
		
	 	for(int i = 0; i<days.size()-1;++i){
			if (days[i]> days[i+1]){
				days.push_back(i),
				months.push_back(temp1);
				years.push_back(temp2);
				appointments.push_back(temp3);
			}
		}
	}


This code does not work. I posted more on the bottom.

Can someone help me with what I am doing wrong?
it says " (int k = 0; k<years.size()-1;++k)" reqs an expression
but I want it all in the same loop, so it increments and moves them all at the same time. How do I do that?

Really new to C++






Trying to figure out how to bubble sort more than 2 things.

Here is my code. I did 1 string, and 1 date. I was able to
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
	
int main ()	
{
	int a = 0;
	vector<string> appointments;
	string appointment;
	vector<int> dates;
	
	
	int date;
	string yes;
	string temp;
	while( a < 99) // limiting you to 99 appointments you HARD WORKING MAN YOU!
	{
		cout << " enter a name for an appointment" <<"\n";
		std::cin >> appointment;
		appointments.push_back (appointment);
		cout << "enter Year month day 'example 2013 04 25' No spaces' like '20130425' 'type a zero first before month #, and same for days' "  <<"\n";
		std::cin >> date;
		dates.push_back (date);

		 
		cout << "Would you like to stop adding appointments? type 'Yes' if you do or 'No' to keep going"  <<"\n";
		cin>>yes;
		if(yes=="Yes"){
			break;
		}
		else{ 
			continue;
		}
		a++;
    }
	for (int j = 0; j<appointments.size()-1;++j){
		temp = appointments[j];
	 	for(int i = 0; i<dates.size()-1;++i){
			if (dates[i]> dates[i+1]){
				dates.push_back(i);
				appointments.push_back(temp);
			}
		}
	}


1
2
3
4
5
6
7
8
9
for (int j = 0; j<appointments.size()-1;++j){
		temp = appointments[j];
	 	for(int i = 0; i<dates.size()-1;++i){
			if (dates[i]> dates[i+1]){
				dates.push_back(i);
				appointments.push_back(temp);
			}
		}
	}


This works




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
	
int main ()	
{
	int a = 0;
	vector<string> appointments;
	string appointment;
	vector<int> years;
	vector<int> months;
	vector<int> days;
	
	int year;
	int month;
	int day;
	string yes;
	string temp1;
	string temp2;
	string temp3;
	while( a < 99) // limiting you to 99 appointments you HARD WORKING MAN YOU!
	{
		cout << " enter a name for an appointment -- 'NO SPACES' --" <<"\n";
		std::cin >> appointment;
		appointments.push_back (appointment);
		cout << "enter Year  example: '2013' "  <<"\n";
		std::cin >> year;
		years.push_back (year);
		cout << "enter month  example: '5' "  <<"\n";
		std::cin >> month;
		months.push_back (month);
		cout << "enter day  example: '15' "  <<"\n";
		std::cin >> day;
		days.push_back (day);

		 
		cout << "Would you like to stop adding appointments? type 'Yes' if you do or 'No' to keep going"  <<"\n";
		cin>>yes;
		if(yes=="Yes"){
			break;
		}
		else{ 
			continue;
		}
		a++;
    }
	
	
	
	
	
	for (int j = 0; j<months.size()-1;++j)
		(int k = 0; k<years.size()-1;++k)		
		(int l = 0; l<appointments.size()-1;++l)		 {
		temp1 = months[j];
		temp2 = years[k];
		temp3 = appointments[l];	
		
	 	for(int i = 0; i<days.size()-1;++i){
			if (days[i]> days[i+1]){
				days.push_back(i),
				months.push_back(temp1);
				years.push_back(temp2);
				appointments.push_back(temp3);
			}
		}
	}


This does not, I split dates into 3 other vectors, years months and days.

TLDR;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	
	for (int j = 0; j<months.size()-1;++j)
		(int k = 0; k<years.size()-1;++k)		
		(int l = 0; l<appointments.size()-1;++l)		 {
		temp1 = months[j];
		temp2 = years[k];
		temp3 = appointments[l];	
		
	 	for(int i = 0; i<days.size()-1;++i){
			if (days[i]> days[i+1]){
				days.push_back(i),
				months.push_back(temp1);
				years.push_back(temp2);
				appointments.push_back(temp3);
			}
		}
	}


This code
Should I do


1
2
3
4
5
6
7
8
9
	for (int j = 0; j<months.size()-1;++j){
		temp1 = months[j];
	 	for(int i = 0; i<days.size()-1;++i){
			if (days[i]> days[i+1]){
				days.push_back(i);
				months.push_back(temp1 );
			}
		}
	}


1
2
3
4
5
6
7
8
9
	for (int k= 0; k<years.size()-1;++k){
		temp2 = years[k];
	 	for(int i = 0; i<days.size()-1;++i){
			if (days[i]> days[i+1]){
				days.push_back(i);
				years.push_back(temp2 );
			}
		}
	}


1
2
3
4
5
6
7
8
9
	for (int l= 0; l<appointments.size()-1;++l){
		temp3 = appointments[l];
	 	for(int i = 0; i<days.size()-1;++i){
			if (days[i]> days[i+1]){
				days.push_back(i);
				appointments.push_back(temp3 );
			}
		}
	}


But would that pushback the days vector 3x every time each of of the oher one gets pushed? cause I don't want it to do that.
I haven't really looked at your code, but you should just do something like this for your loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (int j = 0; j<months.size()-1 && j<years.size()-1 && j<appointments.size()-1;++j)
{
	temp1 = months[j];
	temp2 = years[j];
	temp3 = appointments[j];	
		
	for(int i = 0; i<days.size()-1;++i)
	{
		if (days[i]> days[i+1])
		{
			days.push_back(i),
			months.push_back(temp1);
			years.push_back(temp2);
			appointments.push_back(temp3);
		}
	}
}
@fg109 ah thank you!!!!

its ok if you haven't read it lol, just by reading your first line I i already knew it would fix it haha .

Really thanks again, and I suck so much at this.
O.o


79:26: error: no matching function for call to âVector<int>::push_back(String&)â

This line
-(12)-- > months.push_back(temp1);

EDIT:
lol forgot its a string and not int. Let me see if that fixes it


Why yes... yes it does fix it. Thanks again!
Last edited on
Looks like there are a number of things off actually.

for (int j = 0; j<months.size()-1;++j)

This is your for loop.

1
2
(int k = 0; k<years.size()-1;++k)		
(int l = 0; l<appointments.size()-1;++l)


These are lost souls. I'm guessing you want it to belong with the for loop, but a for loop doesn't work that way. You can just add a for in front of each of those statements (admittedly I have no idea what you're doing with them).

temp1, temp2 and temp3 are type string. months and years are type int. You can't convert an int to a string this way.

1
2
3
temp1 = months[j];
temp2 = years[k];
temp3 = appointments[l];


Again, you can't convert strings to int in this way so the compiler won't let you.

1
2
3
days.push_back(i),
months.push_back(temp1);
years.push_back(temp2);


Try sort() http://www.cplusplus.com/reference/algorithm/sort/

Edit: Late, so late.
Last edited on
@Olysold

Hey Thanks a lot for respond :D.

Using fg109 post worked. Again Thank you all for help!
Topic archived. No new replies allowed.