merging vectors problem.

hi guys i am new at C++ and i am right now learning about vectors. i created 2 vectors which deposit high scores for a game, and while i try to merge them it keeps reporting me an error number 3094. the code i use is.


#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
vector<int>::const_iterator iter;
cout << "Creating a list of scores.";
vector<int> scores;
scores.push_back(7500);
scores.push_back(1500);
scores.push_back(3500);

cout << "High scores: " << endl;
for(iter = scores.begin(); iter != scores.end(); ++iter)
cout << *iter << endl;

cout << "randomizing scores." << endl;
srand(time(0));
random_shuffle(scores.begin(), scores.end());
cout << "High scores: " << endl;
for (iter = scores.begin(); iter != scores.end(); ++iter)
cout << *iter << endl;

cout << "sorting scores." << endl;
sort(scores.begin(), scores.end());
cout << "High scores: " << endl;
for(iter = scores.begin(); iter != scores.end(); ++iter)
cout << *iter << endl;

cout << "Creating more scores." << endl << endl;
vector<int> moreScores;
moreScores.push_back(8000);
moreScores.push_back(5000);
moreScores.push_back(3000);


cout << "Printing more scores." << endl;
vector<int> allScores(8);
for(iter = moreScores.begin(); iter != moreScores.end(); ++iter)
cout << *iter << endl;

cout << "Merging both lists.";
// need container big enough to hold results
merge(scores.begin(), scores.end(), moreScores.begin(), moreScores.end(), allScores.begin());

cout << "all high scores: " << endl;
for (iter = allScores.begin(); iter != allScores.end(); ++iter)
cout << *iter << endl;
return 0;
}

the problem is at the merge cause when i cut it by making it a comment with double slash the program doesnt produce an error..

thanks in advance for the help cheers


Last edited on
Could you copy and paste the error you're getting? Also put your code between [code]code tags[/code]
http://imgur.com/tcHOS this is the error that it prints.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	vector<int>::const_iterator iter;
	cout << "Creating a list of scores.";
	vector<int> scores;
	scores.push_back(7500);
	scores.push_back(1500);
	scores.push_back(3500);

	cout << "High scores: " << endl;
	for(iter = scores.begin(); iter != scores.end(); ++iter)
		cout << *iter << endl;

	cout << "randomizing scores." << endl;
	srand(time(0));
	random_shuffle(scores.begin(), scores.end());
	cout << "High scores: " << endl;
	for (iter = scores.begin(); iter != scores.end(); ++iter)
		cout << *iter << endl;

	cout << "sorting scores." << endl;
	sort(scores.begin(), scores.end());
	cout << "High scores: " << endl;
	for(iter = scores.begin(); iter != scores.end(); ++iter)
		cout << *iter << endl;

	cout << "Creating more scores." << endl << endl;
	vector<int> moreScores;
	moreScores.push_back(8000);
	moreScores.push_back(5000);
	moreScores.push_back(3000);

	
	cout << "Printing more scores." << endl;
	for(iter = moreScores.begin(); iter != moreScores.end(); ++iter)
		cout << *iter << endl;

	cout << "Merging both lists.";
	// need container big enough to hold results
	vector<int> allScores(8);
	
	merge(scores.begin(), scores.end(), moreScores.begin(), moreScores.end(), allScores.begin());

	cout << "all high scores: " << endl;
	for (iter = allScores.begin(); iter != allScores.end(); ++iter)
		cout << *iter << endl;
	return 0;
}


i hope i managed to do it right:O
The error is telling you exactly what you need to know - "sequence not ordered"
See the documentation:
http://www.cplusplus.com/reference/algorithm/merge/
For the function to yield the expected result, the elements in the both ranges shall already be ordered according to the same strict weak ordering criterion (operator< or comp). The resulting range is also sorted according to it

Your scores are backwards, and thus, considered unsorted. If you want them sorted backwards, use the comp parameter with either an object that overloads operator() to return a bool if the first parameter goes before the second, or a function pointer that does the same.
oh man ... thanks for helping me out LB!! your help is really appreciated it worked just fine now.. thanks a lot and best regards:)
Topic archived. No new replies allowed.