bs Lab 13 Create an STL vector of 10 integers and store initial values 0 to 9 (d
ID: 3712201 • Letter: B
Question
bs Lab 13 Create an STL vector of 10 integers and store initial values 0 to 9 (data[0] 0, data[1] Use the std: random shuffle algorithm to shuffle the list. Print the list. (use an iterator in a regular for loop) Use the std: sort algorithm to sort the list Print the list again. (use a range-based for loop) 1, etc.). Optional Challenge: use a predicate to remove the even numbers, then print the list. Which std algorithm should you use? You will need to locate the documentation for random shufle, sort, and the range-based for loop either in Vs help or with Google. Once you leave this class, you will need to be able to find information like this on your own. Checklist 1. Project/solution named correctly 2. Correct comments at top 3. Consistent indentation (Use Edit/Advanced/Format Document) 4. Good variable names 5. Overall neat organization 6. Comments in code explain what's being done 7 Correct division of code into h and cpp files 8 Use of #oragma once in h files (or, #ifndef) 9. #include "stdafx.h"in cpp files (or, suppress pch)Explanation / Answer
///////////////////////////////////////////////////////Cpp_Code////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
using namespace std;
int main()
{
std::vector<int> MyVector;
vector <int> :: iterator i;
for (int i = 0; i < 10; i++){
MyVector.push_back(i);}
cout << "Current Vector Before Randomize : ";
for (i = MyVector.begin(); i != MyVector.end(); ++i)
cout << *i << ' ';
cout << endl << endl;
std::random_shuffle(MyVector.begin(), MyVector.end());
cout << "Current Vector After Randomize : ";
for (i = MyVector.begin(); i != MyVector.end(); ++i)
cout << *i << ' ';
cout << endl << endl;
std::sort (MyVector.begin(), MyVector.end());
cout << "Current Vector After Sorting : ";
for (i = MyVector.begin(); i != MyVector.end(); ++i)
cout << *i << ' ';
cout << endl << endl;
cout << "Removing the even numbers from vector : ";
for (i = MyVector.begin(); i != MyVector.end(); ++i){
if(*i%2 == 0){
MyVector.erase(i);
}
}
for (i = MyVector.begin(); i != MyVector.end(); ++i)
cout << *i << ' ';
cout << endl << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.