Using Vector Functions:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector<int> numbers = {5, 2, 8, 3, 1};

std::sort(numbers.begin(), numbers.end()); // Sorting the vector

for (const auto &number : numbers) {
std::cout << number << ” “; // Displaying the sorted vector
}

return 0;
}

 

Vector of Custom Objects:

#include <iostream>
#include <vector>

class Person {
public:
std::string name;
int age;

Person(std::string name, int age) : name(name), age(age) {}
};

int main() {
std::vector<Person> people;
people.push_back(Person(“Alice”, 25));
people.push_back(Person(“Bob”, 30));

for (const auto &person : people) {
std::cout << “Name: ” << person.name << “, Age: ” << person.age << std::endl;
}

return 0;
}

Top online courses in Teaching & Academics

Related Posts

Leave a Reply