Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. // constructing vectors
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. void showVector(vector<int> vec) {
  7. for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
  8. cout << ' ' << *it;
  9. }
  10. cout << '\n';
  11. }
  12. void inverseVec(vector<int> vec) {
  13.  
  14. }
  15. int main ()
  16. {
  17. int myints[] = {16,2,77,29};
  18. vector<int> myVec (myints, myints + sizeof(myints) / sizeof(int) );
  19. myVec.push_back(-2);
  20.  
  21. cout << "The contents of fifth are:";
  22. // 1st way of printig
  23. for(int i = 0; i < myVec.size(); i++) {
  24. cout << myVec[i] << ' ';
  25. }
  26. cout << endl;
  27.  
  28.  
  29. // Sorting
  30. sort(myVec.begin(), myVec.end());
  31. cout << "Sorted vector is:";
  32. // 2nd way of printing
  33. showVector(myVec);
  34.  
  35. // Pop out
  36. cout << "Lets see the pop back";
  37. myVec.pop_back();
  38. showVector(myVec);
  39.  
  40. // Find
  41. vector<int>::iterator it = find(myVec.begin(), myVec.end(), 29);
  42. if(it == myVec.end()) {
  43. cout << "Nu am gasit elementul" << endl;
  44. } else {
  45. cout << "Am gasit elementul " << *it << " la pozitia: " << it - myVec.begin() << endl;
  46. }
  47. inverseVec(myVec);
  48. showVector(myVec);
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement