Oui, voici 50 autres exemples d'utilisation de la bibliothèque standard C++ :
- Conversion de chaînes en nombres :
std::string str = "42";
int num = std::stoi(str);
- Recherche d'un élément dans un vecteur :
std::vector<int> v = {1, 2, 3, 4, 5};
if (std::find(v.begin(), v.end(), 3) != v.end()) {
std::cout << "Element found\n";
}
- Tri d'un vecteur :
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
std::sort(v.begin(), v.end());
for (auto elem : v) {
std::cout << elem << ' ';
}
- Division entière :
int a = 7;
int b = 3;
int quotient = a / b;
int reste = a % b;
- Génération aléatoire de nombres :
std::uniform_real_distribution<double> distribution(0.0, 1.0);
std::mt19937 engine;
double random = distribution(engine);
- Utilisation de la bibliothèque mathématique :
double x = 2.0;
double y = std::sin(x);
- Cout en hexadécimal :
int num = 42;
std::cout << std::hex << num << '\n';
- Lecture de ligne de commande :
int main(int argc, char** argv) {
std::cout << "You entered " << argc << " arguments\n";
for (int i = 0; i < argc; ++i) {
std::cout << "Argument " << i << ": " << argv[i] << '\n';
}
return 0;
}
- Redimensionnement d'un vecteur :
std::vector<int> v = {1, 2, 3, 4, 5};
v.resize(10, 0);
for (auto elem : v) {
std::cout << elem << ' ';
}
- Création d'une liste chaînée :
struct Node {
int value;
Node* next;
};
Node* head = new Node{1, new Node{2, new Node{3, nullptr}}};
- Utilisation des itérateurs :
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int>::iterator it;
for (it = v.begin(); it != v.end(); ++it) {
std::cout << *it << ' ';
}
- Utilisation des fonctions lambdas :
std::vector<int> v = {1, 2, 3, 4, 5};
std::for_each(v.begin(), v.end(), [](int elem) {
std::cout << elem << ' ';
});
- Traitement des dates et heures :
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::cout << std::ctime(&now_c);
- Utilisation de paires :
std::pair<int, std::string> p = {42, "hello"};
std::cout << p.first << ' ' << p.second << '\n';
- Utilisation de tuples :
std::tuple<int, double, std::string> t = {42, 3.14, "hello"};
std::cout << std::get<0>(t) << ' ' << std::get<1>(t) << ' ' << std::get<2>(t) << '\n';
- Utilisation des ensembles :
std::set<int> s = {1, 2, 3, 4, 5};
std::set<int>::iterator it;
for (it = s.begin(); it != s.end(); ++it) {
std::cout << *it << ' ';
}
- Utilisation des files d'attente :
std::queue<int> q;
q.push(1);
q.push(2);
q.push(3);
std::cout << q.front() << '\n';
q.pop();
std::cout << q.front() << '\n';
- Utilisation des piles :
std::stack<int> s;
s.push(1);
s.push(2);
s.push(3);
std::cout << s.top() << '\n';
s.pop();
std::cout << s.top() << '\n';
- Utilisation des cartes :
#include <iostream>
#include <map>
int main() {
std::map<std::string, int> m = {{"foo", 42}, {"bar", 69}, {"baz", 1337}};
for (auto const& [key, val] : m) {
std::cout << key << " : " << val << '\n';
}
return 0;
}
- Utilisation des algorithmes de recherche binaire :
std::vector<int> v = {1, 2, 3, 4, 5};
if (std::binary_search(v.begin(), v.end(), 3)) {
std::cout << "Element found\n";
}
- Utilisation de piles en double :
#include <iostream>
#include <deque>
int main() {
std::deque<int> d = {1, 2, 3};
d.push_front(0);
d.push_back(4);
for (auto const& elem : d) {
std::cout << elem << ' ';
}
return 0;
}
- Utilisation des ensembles ordonnés :
std::set<int, std::greater<int>> s = {1, 2, 3, 4, 5};
std::set<int, std::greater<int>>::iterator it;
for (it = s.begin(); it != s.end(); ++it) {
std::cout << *it << ' ';
}
- Utilisation des maps triées :
std::map<int, std::string> m = {{3, "three"}, {1, "one"}, {4, "four"}, {2, "two"}};
std::map<int, std::string>::iterator it;
for (it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << " : " << it->second << '\n';
}
- Utilisation des piles à liste doublement chaînées :
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 2, 3};
l.push_front(0);
l.push_back(4);
for (auto const& elem : l) {
std::cout << elem << ' ';
}
return 0;
}
- Utilisation des paires avec auto :
auto p = std::make_pair(42, "hello");
std::cout << p.first << ' ' << p.second << '\n';
- Utilisation de shared_ptr :
std::shared_ptr<int> p1 = std::make_shared<int>(42);
std::shared_ptr<int> p2 = p1;
- Utilisation de weak_ptr :
std::shared_ptr<int> p1 = std::make_shared<int>(42);
std::weak_ptr<int> wp = p1;
std::shared_ptr<int> p2 = wp.lock();
- Utilisation d'emplace_back():
std::vector<std::pair<int, std::string>> v;
v.emplace_back(1, "one");
v.emplace_back(2, "two");
v.emplace_back(3, "three");
for (auto const& elem : v) {
std::cout << elem.first << " : " << elem.second << '\n';
}
- Utilisation de make_tuple():
auto t = std::make_tuple(42, 3.14, "hello");
std::cout << std::get<0>(t) << ' ' << std::get<1>(t) << ' ' << std::get<2>(t) << '\n';
- Utilisation de transform():
std::string str = "42";
std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) { return std::toupper(c); });
std::cout << str << '\n';
- Utilisation de partial_sum():
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> result(v.size());
std::partial_sum(v.begin(), v.end(), result.begin());
for (auto const& elem : result) {
std::cout << elem << ' ';
}
- Utilisation de all_of():
std::vector<int> v = {1, 2, 3, 4, 5};
if (std::all_of(v.begin(), v.end(), [](int elem) { return elem > 0; })) {
std::cout << "All elements are positive\n";
}
- Utilisation de accumulate():
std::vector<int> v = {1, 2, 3, 4, 5};
int result = std::accumulate(v.begin(), v.end(), 0);
std::cout << result << '\n';
- Utilisation de sort() personnalisé :
struct Person {
std::string name;
int age;
};
bool compare(Person const& lhs, Person const& rhs) {
return lhs.age < rhs.age;
}
int main() {
std::vector<Person> v = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 20}};
std::sort(v.begin(), v.end(), compare);
for (auto const& elem : v) {
std::cout << elem.name << " : " << elem.age << '\n';
}
return 0;
}
- Utilisation de find_if():
std::vector<int> v = {1, 2, 3, 4, 5};
auto it = std::find_if(v.begin(), v.end(), [](int elem) { return elem > 3; });
if (it != v.end()) {
std::cout << "Element found: " << *it << '\n';
}
- Utilisation de is_permutation():
std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = {4, 2, 1, 5, 3};
if (std::is_permutation(v1.begin(), v1.end(), v2.begin())) {
std::cout << "Vectors are permutations of each other\n";
}
- Utilisation de inplace_merge():
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
std::vector<int>::iterator mid = v.begin() + 5;
std::inplace_merge(v.begin(), mid, v.end());
for (auto const& elem : v) {
std::cout << elem << ' ';
}
- Utilisation de remove_copy_if():
std::vector<int> v = {1, 2, -3, 4, -5};
std::vector<int> result(v.size());
std::remove_copy_if(v.begin(), v.end(), result.begin(), [](int elem) { return elem < 0; });
for (auto const& elem : result) {
std::cout << elem << ' ';
}
- Utilisation de partition():
std::vector<int> v = {1, 2, -3, 4, -5};
std::partition(v.begin(), v.end(), [](int elem) { return elem > 0; });
for (auto const& elem : v) {
std::cout << elem << ' ';
}
- Utilisation de next_permutation():
std::vector<int> v = {1, 2, 3};
std::sort(v.begin(), v.end());
do {
for (auto const& elem : v) {
std::cout << elem << ' ';
}
std::cout << '\n';
} while (std::next_permutation(v.begin(), v.end()));
- Utilisation de set_intersection():
std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = {4, 5, 6, 7, 8};
std::vector<int> result(std::min(v1.size(), v2.size()));
auto it = std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin());
result.resize(it - result.begin());
for (auto const& elem : result) {
std::cout << elem << ' ';
}
- Utilisation de nth_element():
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
std::vector<int>::iterator nth = v.begin() + 5;
std::nth_element(v.begin(), nth, v.end());
std::cout << "The fifth element is " << *nth << '\n';
- Utilisation de rotate():
std::vector<int> v = {1, 2, 3, 4, 5};
std::rotate(v.begin(), v.begin() + 2, v.end());
for (auto const& elem : v) {
std::cout << elem << ' ';
}
- Utilisation de is_heap():
std::vector<int> v = {1, 2, 3, 4, 5};
std::make_heap(v.begin(), v.end());
if (std::is_heap(v.begin(), v.end())) {
std::cout << "Vector is a heap\n";
}
- Utilisation de count_if():
std::vector<int> v = {1, 2, 3, 4, 5};
int result = std::count_if(v.begin(), v.end(), [](int elem) { return elem > 3; });
std::cout << result << '\n';
- Utilisation de minmax_element():
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
auto it = std::minmax_element(v.begin(), v.end());
std::cout << "Minimum is " << *it.first << ", maximum is " << *it.second << '\n';
- Utilisation de find_end():
std::vector<int> v = {1, 2, 3, 4, 1, 2, 3, 4, 5};
std::vector<int> pattern = {1, 2, 3};
auto it = std::find_end(v.begin(), v.end(), pattern.begin(), pattern.end());
if (it != v.end()) {
std::cout << "Pattern found at index " << it - v.begin() << '\n';
}
- Utilisation de adjacent_find():
std::vector<int> v = {1, 2, 3, 4, 4, 5, 6, 7};
auto it = std::adjacent_find(v.begin(), v.end());
if (it != v.end()) {
std::cout << "Adjacent pair found at index " << it - v.begin() << '\n';
}
- Utilisation de find_first_of():
std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> pattern = {4, 5};
auto it = std::find_first_of(v.begin(), v.end(), pattern.begin(), pattern.end());
if (it != v.end()) {
std::cout << "Pattern found at index " << it - v.begin() << '\n';
}
- Utilisation de is_sorted():
std::vector<int> v = {1, 2, 3, 4, 5};
if (std::is_sorted(v.begin(), v.end())) {
std::cout << "Vector is sorted\n";
}