0% found this document useful (0 votes)
2 views

Lab 7

Uploaded by

singh.angad0905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 7

Uploaded by

singh.angad0905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Lab 7

Objective: The purpose of this lab is to practice working with std::vector in C++.
Use the following reference to finish the tasks listed below

https://www.w3schools.com/cpp/cpp_ref_vector.asp

Task 1: Working with Integer Vectors


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};

// Add 60 to the end of the vector, use push_back()

// Insert 25 between 20 and 30, use insert()

// Sort in descending order


sort(numbers.begin(), numbers.end());

// Print the result


cout << "Sorted Vector: ";
for (int num : numbers) {
cout << num << " ";
}
cout << endl;

return 0;
}

Task 2: String Vector Operations

• Create a vector of strings called words and initialize it with values such as {"apple", "banana", "cherry",
"date"}.
• Add "elderberry" to the vector.
• Sort the vector in alphabetical order.
• Search for "cherry" and print its position in the vector.
• Remove "banana" from the vector and print the final list of words.

Submission:
You need to upload to Moodle: the source code (.cpp files), and the screenshot of the running of your program.

You might also like