Vectors and Algorithm

Hi guys, I am having problems with vectors. I am suppose to create a vector with 10 objects, which I have done, shuffle the vector and print it. Then I have to add two more numbers using the push_back method, which is where I am having problems. Using the algorithm class I have to use binary search to look for 5 and print and then search for -1. Basically I have to do a lot of things and my teacher didn't quite explain it properly. I really need help and if someone could explain it that would be wonderful. Oh and by the way, Pikachu is the name of my vector.

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

using namespace std;

int main(){
	std::vector <int> Pikachu(10);
	for (int i = 0; i < 10; ++i)
	{
		int r = i + rand() % (10 - i);
		swap(Pikachu[i], Pikachu[r]);
		Pikachu[i] = i;
	}
	for (int i = 0; i < 10; ++i)
	{
		cout << Pikachu[i] << endl;
	}
	Pikachu.push_back(2);
	std::sort(Pikachu.begin(), Pikachu.end());
	for (int i = 0; i < 10; ++i)
	{
		cout << Pikachu[i] << endl;
	}
	
	
	
	
	
	cout << "Coded By: My Name" << endl;
	system("pause");
	return 0;
}
Last edited on
Then I have to add two more numbers using the push_back method, which is where I am having problems.


push_back simply adds the value you give it to the end of the vector:

1
2
3
4
5
6
std::vector<int> foo;  // foo is empty

foo.push_back(3);    // foo contains:  3
foo.push_back(100);  // foo contains:  3, 100
foo.push_back(5);    // foo contains:  3, 100, 5
// etc 


Using the algorithm class I have to use binary search to look for 5 and print and then search for -1.


Err... a binary search won't work on a randomly shuffled vector. The vector would need to be sorted. Are you sure you are understanding the instructions correctly? Or is this for a different assignment altogether?
Last edited on
Here are my instructions:
You can use a loop to fill it with the numbers 1 – 10. Using the algorithm class, shuffle the contents of the vector. Print the vector (use a loop). Add two more numbers to the vector with the push_back method. Using the algorithm class, sort the contents of the vector and print the vector again. Use a binary search (from algorithm class) to search for a 5 (or some value in the vector) and then search for -1(test not found). Print the results after each search. Choose three other algorithm functions and use then on the vector object. Show the results by printing the vector or printing an appropriate message.
Using the algorithm class, sort the contents of the vector and print the vector again. Use a binary search (from algorithm class) to search for a 5 (or some value in the vector)



Okay yeah -- so you have to sort it again. That makes more sense.

Anyway, this page has documentation (with examples) of how to do a sort and binary search:

http://www.cplusplus.com/reference/algorithm/ <- algorithm header
http://www.cplusplus.com/reference/algorithm/sort/ <- sort
http://www.cplusplus.com/reference/algorithm/binary_search/ <- binary search
@Disch. He does sort it again if you look at his code.

std::sort(Pikachu.begin(), Pikachu.end());
Okay thank you. I will have a look and see if this can help me.
@TarikNeaj I am a lady. And I followed the example and it doesn't recognize "it".

1
2
3
4
5
std::sort(Pikachu.begin(), Pikachu.end());
	std::cout << "Pikachu Contains:" << endl;
	for (std::vector<int>iterator it=Pikachu.begin(); it!=Pikachu.end(); ++iterator)
		std::cout << ' ' << *it;
	std::cout << "\n";
Alright my lady. Its because you're declaring the iterator wrong and ++iterator wont work either becuase you named your iterator "it", so you wanna do ++it.

Here is how its done -

std::vector<int>::iterator it // notice the :: just before iterator

Here's the full code -

1
2
3
4
5
std::sort(Pikachu.begin(), Pikachu.end());
	std::cout << "Pikachu Contains:" << endl;
	for (std::vector<int>::iterator it = Pikachu.begin(); it != Pikachu.end(); ++it)
		std::cout << ' ' << *it;
	std::cout << "\n";

Also. There is a feature in c++11 called auto. So you can use that one instead of writing the suuuuper long iterator thingy.

for (auto it = Pikachu.begin(); it != Pikachu.end(); ++it)
Last edited on
@TarikNeaj Yay it works! I will definitely remember :: next time and also the auto code. I still got some ways to go before I can mark this thread as solved v.v Thanks for helping me.
Not to confuse you, but there's also this....

1
2
3
4
5
6
7
8
9
10
11
// This:
for (std::vector<int>::iterator it = Pikachu.begin(); it != Pikachu.end(); ++it)
    std::cout << ' ' << *it;

// Is the same as this:
for (auto it = Pikachu.begin(); it != Pikachu.end(); ++it)
    std::cout << ' ' << *it;

// Is also the same as this:
for (auto& i : Pikachu)
    std::cout << ' ' << i;


TarikNeaj pointed out the use of auto in the 2nd example, but the 3rd example is usually much easier. It's a "range based for loop" and was also introduced in C++11. All major compilers support it (unless you're running something really outdated)
Ahh Thank you @Disch. I knew there was a muuuch simpler way of using auto in this case, just didint know what. Thank you again :)
Thank you for helping me. It runs but it gives me a lot of zeros before 1-9. Plus I have to do this:
Create another vector of 10 positions, fill it with numbers from 1-10 (any method) and sort it into ascending sequence using algorithm class, as above.
Resort vector 1 into ascending sequence
Merge the contents of the two vectors into a third vector. Use the following merge algorithm.
get value1 from v1
get value2 from v2

I have no idea on how to initialize a Pikachu (value1) to v1 and Ashe (value2) to v2.

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
int value1;
	int value2;
	std::vector <int> Pikachu(10);
	std::vector <int> Ashe(10);
	std::vector <int> Pokeball(10);
	for (int i = 0; i < 10; ++i)
		Pikachu.push_back(i);
	cout << "Before the shuffle:" << endl;
	std::random_shuffle(Pikachu.begin(), Pikachu.end());
	for (std::vector<int>::iterator it = Pikachu.begin(); it != Pikachu.end(); ++it)
		std::cout << ' ' << *it;
	cout << endl;
	cout << "\n" << endl;
	Pikachu.push_back(2);
	std::sort(Pikachu.begin(), Pikachu.end());
	std::cout << "Pikachu Contains:" << endl;
	for (std::vector<int>::iterator it = Pikachu.begin(); it != Pikachu.end(); ++it)
	{
		std::cout << ' ' << *it;
	}
	cout << endl;
	cout << "\n" << endl;
	std::cout << "Looking for 5..." << endl;
	if (std::binary_search(Pikachu.begin(), Pikachu.end(), 5))
		std::cout << "Found it!" << endl;
	else
		std::cout << "Not there sorry!" << endl;
	cout << "\n" << endl;

	std::cout << "Looking for -1..." << endl;
	if (std::binary_search(Pikachu.begin(), Pikachu.end(), -1))
		std::cout << "Found it!" << endl;
	else
		std::cout << "Not there sorry!" << endl;
	cout << "\n" << endl;
	for (int j = 1; j < 10; ++j)
		Ashe.push_back(j);
	std::sort(Ashe.begin(), Ashe.end());
	std::cout << "Ashe Contains:" << endl;
	for (std::vector<int>::iterator it = Ashe.begin(); it != Ashe.end(); ++it)
	{
		std::cout << ' ' << *it;
	}
	cout << endl;

	

	cout << "\n" << endl;
	cout << "Coded By: My Name" << endl;
	system("pause");
	return 0;
Topic archived. No new replies allowed.