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.
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?
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:
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)
@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.
// 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)
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.