From 6ace46a024bd59073d34e11f7a4a3cd23ee1f793 Mon Sep 17 00:00:00 2001 From: PRATHAMESH CHOUGALE <106848418+Prathamesh-chougale-17@users.noreply.github.com> Date: Tue, 31 Oct 2023 15:46:37 +0530 Subject: [PATCH 1/3] Important STL properties and function with example --- .../Important_stl_and_function_array.cpp | 169 ++++++++ .../Important_stl_and_function_string.cpp | 192 +++++++++ .../Important_stl_and_function_vector.cpp | 389 ++++++++++++++++++ .../important_function_of_sets.cpp | 78 ++++ 4 files changed, 828 insertions(+) create mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp create mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp create mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp create mode 100644 Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp new file mode 100644 index 00000000000..28748a4ea93 --- /dev/null +++ b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp @@ -0,0 +1,169 @@ +#include +using namespace std; +int main() +{ + int a[10] = {11, 23, 53, 64, 75, 16, 27, 88, 69, 10}; // array of size 10 + + // 1. sort + sort(a, a + 10); // sort(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 2. reverse + reverse(a, a + 10); // reverse(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 3. min_element + cout << *min_element(a, a + 10) << endl; // min_element(starting address, ending address) + + // 4. max_element + cout << *max_element(a, a + 10) << endl; // max_element(starting address, ending address) + + // 5. accumulate + cout << accumulate(a, a + 10, 0) << endl; // accumulate(starting address, ending address, initial value) + + // 6. count + cout << count(a, a + 10, 10) << endl; // count(starting address, ending address, element to be counted) + + // 7. find + cout << *find(a, a + 10, 10) << endl; // find(starting address, ending address, element to be found) + + // 8. binary_search + cout << binary_search(a, a + 10, 10) << endl; // binary_search(starting address, ending address, element to be found) + + // 9. lower_bound + cout << *lower_bound(a, a + 10, 10) << endl; // lower_bound(starting address, ending address, element to be found) + + // 10. upper_bound + cout << *upper_bound(a, a + 10, 10) << endl; // upper_bound(starting address, ending address, element to be found) + + // 11. rotate + rotate(a, a + 3, a + 10); // rotate(starting address, new starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 12. next_permutation + next_permutation(a, a + 10); // next_permutation(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 13. prev_permutation + prev_permutation(a, a + 10); // prev_permutation(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 14. swap + swap(a[0], a[9]); // swap(element 1, element 2) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 15. swap_ranges + swap_ranges(a, a + 5, a + 5); // swap_ranges(starting address 1, ending address 1, starting address 2) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 16. reverse_copy + reverse_copy(a, a + 10, a); // reverse_copy(starting address, ending address, starting address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 17. rotate_copy + rotate_copy(a, a + 3, a + 10, a); // rotate_copy(starting address, new starting address, ending address, starting address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 18. is_sorted + cout << is_sorted(a, a + 10) << endl; // is_sorted(starting address, ending address) + + // 19. is_partitioned + cout << is_partitioned(a, a + 10, [](int x) + { return x % 2 == 0; }) + << endl; // is_partitioned(starting address, ending address, condition) + + // 20. partition + partition(a, a + 10, [](int x) + { return x % 2 == 0; }); // partition(starting address, ending address, condition) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 21. stable_partition + stable_partition(a, a + 10, [](int x) + { return x % 2 == 0; }); // stable_partition(starting address, ending address, condition) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 22. partition_point + auto it = partition_point(a, a + 10, [](int x) + { return x % 2 == 0; }); // partition_point(starting address, ending address, condition) + cout << *it << endl; + + // 23. random_shuffle + random_shuffle(a, a + 10); // random_shuffle(starting address, ending address) + + // 24. shuffle + shuffle(a, a + 10, default_random_engine()); // shuffle(starting address, ending address, random engine) + + // 25. max + cout << max(2, 3) << endl; // max(element 1, element 2) + + // 26. min + cout << min(2, 3) << endl; // min(element 1, element 2) + + // 27. memset + int b[10]; + memset(b, 0, sizeof(b)); // memset(starting address, value to be filled, size of the array) + + // 28. is_permutation + cout << is_permutation(a, a + 10, b) << endl; // is_permutation(starting address 1, ending address 1, starting address 2) + + // 29. next + auto it = std::next(std::begin(a), 1); // get iterator to second element of array a + + // 30. prev + auto it = std::prev(std::end(a), 1); // get iterator to last element of array a + + // 31. iota + iota(a, a + 10, 1); // iota(starting address, ending address, value to be filled) + + // 32. gcd + cout << __gcd(10, 20) << endl; // __gcd(element 1, element 2)//info: __gcd is a inbuilt function for finding gcd of two numbers + + // 33. lcm + cout << lcm(10, 20) << endl; // lcm(element 1, element 2) + + return 0; +} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp new file mode 100644 index 00000000000..02c5173aa28 --- /dev/null +++ b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp @@ -0,0 +1,192 @@ +#include +using namespace std; +int main() +{ + string s = "Hello World"; + cout << s << endl; + + // 1. sort + sort(s.begin(), s.end()); // sort(starting address, ending address) + cout << s << endl; + + // 2. reverse + reverse(s.begin(), s.end()); // reverse(starting address, ending address) + cout << s << endl; + + // 3. min_element + cout << *min_element(s.begin(), s.end()) << endl; // min_element(starting address, ending address) + + // 4. max_element + cout << *max_element(s.begin(), s.end()) << endl; // max_element(starting address, ending address) + + // 5. accumulate + cout << accumulate(s.begin(), s.end(), 0) << endl; // accumulate(starting address, ending address, initial value)//info: accumulate() is used to find the sum of elements in a container. + + // 6. count + cout << count(s.begin(), s.end(), 'l') << endl; // count(starting address, ending address, element to be counted) //info: count() is used to count the number of occurrences of an element in a container. + + // 7. find + cout << *find(s.begin(), s.end(), 'l') << endl; // find(starting address, ending address, element to be found)//info: find() is used to find the first occurrence of an element in a container. + + // 8. binary_search + cout << binary_search(s.begin(), s.end(), 'l') << endl; // binary_search(starting address, ending address, element to be found) + + // 9. lower_bound + cout << *lower_bound(s.begin(), s.end(), 'l') << endl; // lower_bound(starting address, ending address, element to be found)//info: lower_bound() is used to find the position of the first element in a container which is not less than a specified value. + + // 10. upper_bound + cout << *upper_bound(s.begin(), s.end(), 'l') << endl; // upper_bound(starting address, ending address, element to be found) + + // 11. rotate + rotate(s.begin(), s.begin() + 3, s.end()); // rotate(starting address, new starting address, ending address) + cout << s << endl; + + // 12. next_permutation + next_permutation(s.begin(), s.end()); // next_permutation(starting address, ending address) + cout << s << endl; + + // 13. substr + cout << s.substr(3, 4) << endl; // substr(starting index, length) + + // 14. find_first_of + cout << s.find_first_of("l") << endl; // find_first_of(string to be found) + + // 15. find_last_of + cout << s.find_last_of("l") << endl; // find_last_of(string to be found) + + // 16. find_first_not_of + cout << s.find_first_not_of("l") << endl; // find_first_not_of(string to be found) + + // 17. find_last_not_of + cout << s.find_last_not_of("l") << endl; // find_last_not_of(string to be found) + + // 18. stoi + string s1 = "123"; + cout << stoi(s1) << endl; // stoi(string to be converted) + + // 19. to_string + int x = 123; + cout << to_string(x) << endl; // to_string(int to be converted) + + // 20. isdigit + cout << isdigit(s[0]) << endl; // isdigit(character to be checked) + + // 21. isalpha + cout << isalpha(s[0]) << endl; // isalpha(character to be checked) + + // 22. isalnum + cout << isalnum(s[0]) << endl; // isalnum(character to be checked) + + // 23. islower + cout << islower(s[0]) << endl; // islower(character to be checked) + + // 24. isupper + cout << isupper(s[0]) << endl; // isupper(character to be checked) + + // 25. isxdigit + cout << isxdigit(s[0]) << endl; // isxdigit(character to be checked) + + // 26. isgraph + cout << isgraph(s[0]) << endl; // isgraph(character to be checked) + + // 27. isprint + cout << isprint(s[0]) << endl; // isprint(character to be checked) + + // 28. iscntrl + cout << iscntrl(s[0]) << endl; // iscntrl(character to be checked) + + // 29. isspace + cout << isspace(s[0]) << endl; // isspace(character to be checked) + + // 30. isblank + cout << isblank(s[0]) << endl; // isblank(character to be checked) + + // 31. ispunct + cout << ispunct(s[0]) << endl; // ispunct(character to be checked) + + // 32. toupper + cout << toupper(s[0]) << endl; // toupper(character to be converted) + + // 33. tolower + cout << tolower(s[0]) << endl; // tolower(character to be converted) + + // 34. getline + getline(cin, s); // getline(input stream, string) + + // 35. stringstream + stringstream ss; + ss << s; // ss << string + ss >> s; // ss >> string + + // 36. stringstream + stringstream ss1(s); // ss(string) + + // 37. stringstream + stringstream ss2(s, ios_base::in | ios_base::out); // ss(string, mode) + + // 38. stringstream + stringstream ss3(s, ios_base::in); // ss(string, mode) + + // 39. stringstream + stringstream ss4(s, ios_base::out); // ss(string, mode) + + // 40. stringstream + stringstream ss5(ios_base::in | ios_base::out); // ss(mode) + + // 41. stringstream + stringstream ss6(ios_base::in); // ss(mode) + + // 42. stringstream + stringstream ss7(ios_base::out); // ss(mode) + + // 43. stringstream + stringstream ss8; // ss + + // 44. stringstream + stringstream ss9(ios_base::app); // ss(mode) + + // 45. stringstream + stringstream ss10(ios_base::ate); // ss(mode) + + // 46. stringstream + stringstream ss11(ios_base::binary); // ss(mode) + + // 47. stringstream + stringstream ss12(ios_base::trunc); // ss(mode) + + // 48. stringstream + stringstream ss13(ios_base::in | ios_base::out | ios_base::binary); // ss(mode) + + // 49. stringstream + stringstream ss14(ios_base::in | ios_base::out | ios_base::ate); // ss(mode) + + // 50. stringstream + stringstream ss15(ios_base::in | ios_base::out | ios_base::app); // ss(mode) + + // 51. stringstream + stringstream ss16(ios_base::in | ios_base::out | ios_base::trunc); // ss(mode) + + // 52. stringstream + stringstream ss17(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate); // ss(mode) + + // 53. stringstream + stringstream ss18(ios_base::in | ios_base::out | ios_base::binary | ios_base::app); // ss(mode) + + // 54. stringstream + stringstream ss19(ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc); // ss(mode) + + // 55. stringstream + stringstream ss20(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::app); // ss(mode) + + // 56. stringstream + stringstream ss21(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::trunc); // ss(mode) + + // 57. stringstream + stringstream ss22(ios_base::in | ios_base::out | ios_base::binary | ios_base::app | ios_base::trunc); // ss(mode) + + // 58. stringstream + stringstream ss23(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::app | ios_base::trunc); // ss(mode) + + // 59. + return 0; +} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp new file mode 100644 index 00000000000..b80e7a1429a --- /dev/null +++ b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp @@ -0,0 +1,389 @@ +#include +using namespace std; +int main() +{ + vector v5 = {11, 32, 53, 24, 65}; + // sort + cout << "sort" << endl; + sort(v5.begin(), v5.end()); // sort in ascending order + for (auto i : v5) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + sort(v5.begin(), v5.end(), greater()); // sort in descending order + for (auto i : v5) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // permutation + + vector v = {1, 2, 3, 4, 5}; + cout << "permutation" << endl; + // prev_permutation + prev_permutation(v.begin(), v.end()); // 1 2 3 5 4 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // next_permutation + next_permutation(v.begin(), v.end()); // 1 2 3 5 4 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // rotate + cout << "rotate" << endl; + rotate(v.begin(), v.begin() + 2, v.end()); // 3 5 4 1 2 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // reverse + cout << "reverse" << endl; + reverse(v.begin(), v.end()); // 2 1 4 5 3 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // accumulate + cout << "accumulate" << endl; + int sum = accumulate(v.begin(), v.end(), 0); // 15 + cout << sum << endl; + cout << "----------------------------------------------------------------------" << endl; + + // count + cout << "count" << endl; + int cnt = count(v.begin(), v.end(), 1); // 1 + cout << cnt << endl; + cout << "----------------------------------------------------------------------" << endl; + + // find + cout << "find" << endl; + auto it = find(v.begin(), v.end(), 1); // 1 + cout << *it << endl; + cout << "----------------------------------------------------------------------" << endl; + + // binary_search + cout << "binary_search" << endl; + bool present = binary_search(v.begin(), v.end(), 1); // 1 + cout << present << endl; + cout << "----------------------------------------------------------------------" << endl; + + // lower_bound + cout << "lower_bound" << endl; + auto it = lower_bound(v.begin(), v.end(), 1); // 1 + cout << *it << endl; + cout << "----------------------------------------------------------------------" << endl; + + // upper_bound + cout << "upper_bound" << endl; + auto it = upper_bound(v.begin(), v.end(), 1); // 1 + cout << *it << endl; + cout << "----------------------------------------------------------------------" << endl; + + // max_element + cout << "max_element" << endl; + auto it = max_element(v.begin(), v.end()); // 5 + cout << *it << endl; + cout << "----------------------------------------------------------------------" << endl; + + // min_element + cout << "min_element" << endl; + auto it = min_element(v.begin(), v.end()); // 1 + cout << *it << endl; + cout << "----------------------------------------------------------------------" << endl; + + // minmax_element + cout << "minmax_element" << endl; + auto it = minmax_element(v.begin(), v.end()); // 1 5 + auto it1 = v.begin(); + auto it2 = v.end() - 1; + cout << *it1 << " " << *it2 << endl; + cout << "----------------------------------------------------------------------" << endl; + + // is_sorted + cout << "is_sorted" << endl; + bool sorted = is_sorted(v.begin(), v.end()); // 0 + cout << sorted << endl; + cout << "----------------------------------------------------------------------" << endl; + + // is_sorted_until + cout << "is_sorted_until" << endl; + auto it = is_sorted_until(v.begin(), v.end()); // 1 2 + cout << *it << endl; + cout << "----------------------------------------------------------------------" << endl; + + // is_permutation + cout << "is_permutation" << endl; + vector v1 = {1, 2, 3, 4, 5}; + vector v2 = {1, 2, 3, 4, 5}; + bool permutation = is_permutation(v1.begin(), v1.end(), v2.begin()); // 1 + cout << permutation << endl; + cout << "----------------------------------------------------------------------" << endl; + + // next_permutation + cout << "next_permutation" << endl; + next_permutation(v.begin(), v.end()); // 1 2 3 5 4 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // prev_permutation + cout << "prev_permutation" << endl; + prev_permutation(v.begin(), v.end()); // 1 2 3 4 5 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // swap + cout << "swap" << endl; + swap(v[0], v[1]); // 2 1 3 4 5 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // swap_ranges + cout << "swap_ranges" << endl; + swap_ranges(v.begin(), v.begin() + 2, v.end()); // 3 4 5 1 2 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // transform + cout << "transform" << endl; + transform(v.begin(), v.end(), v.begin(), [](int x) + { return x * x; }); // 9 16 25 1 4 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // fill + cout << "fill" << endl; + fill(v.begin(), v.end(), 5); // 5 5 5 5 5 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // remove + cout << "remove" << endl; + remove(v.begin(), v.end(), 5); // 5 5 5 5 5 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // remove_if + cout << "remove_if" << endl; + remove_if(v.begin(), v.end(), [](int x) + { return x % 2 == 0; }); // 5 5 5 5 5 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // remove_copy + cout << "remove_copy" << endl; + vector v1(5); + remove_copy(v.begin(), v.end(), v1.begin(), 5); // 5 5 5 5 5 + for (auto i : v1) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // remove_copy_if + cout << "remove_copy_if" << endl; + vector v1(5); + remove_copy_if(v.begin(), v.end(), v1.begin(), [](int x) + { return x % 2 == 0; }); // 5 5 5 5 5 + for (auto i : v1) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // replace + cout << "replace" << endl; + replace(v.begin(), v.end(), 5, 1); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // replace_if + cout << "replace_if" << endl; + replace_if( + v.begin(), v.end(), [](int x) + { return x % 2 == 0; }, + 1); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // replace_copy + cout << "replace_copy" << endl; + vector v1(5); + replace_copy(v.begin(), v.end(), v1.begin(), 5, 1); // 1 1 1 1 1 + for (auto i : v1) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // replace_copy_if + cout << "replace_copy_if" << endl; + vector v1(5); + replace_copy_if( + v.begin(), v.end(), v1.begin(), [](int x) + { return x % 2 == 0; }, + 1); // 1 1 1 1 1 + for (auto i : v1) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // reverse + cout << "reverse" << endl; + reverse(v.begin(), v.end()); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // reverse_copy + cout << "reverse_copy" << endl; + vector v1(5); + reverse_copy(v.begin(), v.end(), v1.begin()); // 1 1 1 1 1 + for (auto i : v1) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // rotate_copy + cout << "rotate_copy" << endl; + vector v1(5); + rotate_copy(v.begin(), v.begin() + 2, v.end(), v1.begin()); // 1 1 1 1 1 + for (auto i : v1) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // random_shuffle + cout << "random_shuffle" << endl; + random_shuffle(v.begin(), v.end()); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // shuffle + cout << "shuffle" << endl; + shuffle(v.begin(), v.end(), default_random_engine()); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // unique + cout << "unique" << endl; + unique(v.begin(), v.end()); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // unique_copy + cout << "unique_copy" << endl; + vector v1(5); + unique_copy(v.begin(), v.end(), v1.begin()); // 1 1 1 1 1 + for (auto i : v1) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // is_partitioned + cout << "is_partitioned" << endl; + cout << is_partitioned(v.begin(), v.end(), [](int x) + { return x % 2 == 0; }) + << endl; // 1 1 1 1 1 + cout << "----------------------------------------------------------------------" << endl; + + // partition + cout << "partition" << endl; + partition(v.begin(), v.end(), [](int x) + { return x % 2 == 0; }); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // partition_copy + cout << "partition_copy" << endl; + vector v1(5), v2(5); + partition_copy(v.begin(), v.end(), v1.begin(), v2.begin(), [](int x) + { return x % 2 == 0; }); // 1 1 1 1 1 + for (auto i : v1) + cout << i << " "; + cout << endl; + for (auto i : v2) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // stable_partition + cout << "stable_partition" << endl; + stable_partition(v.begin(), v.end(), [](int x) + { return x % 2 == 0; }); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // partition_point + + cout << "partition_point" << endl; + cout << *partition_point(v.begin(), v.end(), [](int x) + { return x % 2 == 0; }) + << endl; // 1 1 1 1 1 + cout << "----------------------------------------------------------------------" << endl; + + // stable_sort + cout << "stable_sort" << endl; + stable_sort(v.begin(), v.end()); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // partial_sort + cout << "partial_sort" << endl; + partial_sort(v.begin(), v.begin() + 2, v.end()); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // nth_element + cout << "nth_element" << endl; + nth_element(v.begin(), v.begin() + 2, v.end()); // 1 1 1 1 1 + for (auto i : v) + cout << i << " "; + cout << endl; + cout << "----------------------------------------------------------------------" << endl; + + // is_present + cout << "is_present" << endl; + cout << binary_search(v.begin(), v.end(), 3) << endl; // 1 1 1 1 1 + cout << "----------------------------------------------------------------------" << endl; + + return 0; +} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp b/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp new file mode 100644 index 00000000000..0cdb8920a60 --- /dev/null +++ b/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +int main() +{ + set s; + s.insert(10); + s.insert(5); + s.insert(20); + + // 1. sort + for (auto it = s.begin(); it != s.end(); it++) + { + cout << *it << " "; + } + + cout << endl; + + // 2. reverse + for (auto it = s.rbegin(); it != s.rend(); it++) + { + cout << *it << " "; + } + + cout << endl; + + // 3. min_element + cout << *min_element(s.begin(), s.end()) << endl; + + // 4. max_element + cout << *max_element(s.begin(), s.end()) << endl; + + // 5. accumulate + cout << accumulate(s.begin(), s.end(), 0) << endl; + + // 6. count + cout << count(s.begin(), s.end(), 10) << endl; + + // 7. find + cout << *find(s.begin(), s.end(), 10) << endl; + + // 8. binary_search + cout << binary_search(s.begin(), s.end(), 10) << endl; + + // 9. lower_bound + cout << *lower_bound(s.begin(), s.end(), 10) << endl; + + // 10. upper_bound + cout << *upper_bound(s.begin(), s.end(), 10) << endl; + + // // 11. rotate + // rotate(s.begin(), next(s.begin()), s.end()); + + // for (auto it = s.begin(); it != s.end(); it++) + // { + // cout << *it << " "; + // } + + // cout << endl; + + // // 12. next_permutation + // next_permutation(s.begin(), s.end()); + // for (auto it = s.begin(); it != s.end(); it++) + // { + // cout << *it << " "; + // } + + // cout << endl; + + // // 13. prev_permutation + // prev_permutation(s.begin(), s.end()); + // for (auto it = s.begin(); it != s.end(); it++) + // { + // cout << *it << " "; + // } + + return 0; +} \ No newline at end of file From 639c2b870e3f15f7931a2089e2f0f177e830acc6 Mon Sep 17 00:00:00 2001 From: PRATHAMESH CHOUGALE <106848418+Prathamesh-chougale-17@users.noreply.github.com> Date: Tue, 31 Oct 2023 16:01:28 +0530 Subject: [PATCH 2/3] Delete Add Code Here/C++/Important_STL_properties directory --- .../Important_stl_and_function_array.cpp | 169 -------- .../Important_stl_and_function_string.cpp | 192 --------- .../Important_stl_and_function_vector.cpp | 389 ------------------ .../important_function_of_sets.cpp | 78 ---- 4 files changed, 828 deletions(-) delete mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp delete mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp delete mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp delete mode 100644 Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp deleted file mode 100644 index 28748a4ea93..00000000000 --- a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp +++ /dev/null @@ -1,169 +0,0 @@ -#include -using namespace std; -int main() -{ - int a[10] = {11, 23, 53, 64, 75, 16, 27, 88, 69, 10}; // array of size 10 - - // 1. sort - sort(a, a + 10); // sort(starting address, ending address) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 2. reverse - reverse(a, a + 10); // reverse(starting address, ending address) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 3. min_element - cout << *min_element(a, a + 10) << endl; // min_element(starting address, ending address) - - // 4. max_element - cout << *max_element(a, a + 10) << endl; // max_element(starting address, ending address) - - // 5. accumulate - cout << accumulate(a, a + 10, 0) << endl; // accumulate(starting address, ending address, initial value) - - // 6. count - cout << count(a, a + 10, 10) << endl; // count(starting address, ending address, element to be counted) - - // 7. find - cout << *find(a, a + 10, 10) << endl; // find(starting address, ending address, element to be found) - - // 8. binary_search - cout << binary_search(a, a + 10, 10) << endl; // binary_search(starting address, ending address, element to be found) - - // 9. lower_bound - cout << *lower_bound(a, a + 10, 10) << endl; // lower_bound(starting address, ending address, element to be found) - - // 10. upper_bound - cout << *upper_bound(a, a + 10, 10) << endl; // upper_bound(starting address, ending address, element to be found) - - // 11. rotate - rotate(a, a + 3, a + 10); // rotate(starting address, new starting address, ending address) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 12. next_permutation - next_permutation(a, a + 10); // next_permutation(starting address, ending address) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 13. prev_permutation - prev_permutation(a, a + 10); // prev_permutation(starting address, ending address) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 14. swap - swap(a[0], a[9]); // swap(element 1, element 2) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 15. swap_ranges - swap_ranges(a, a + 5, a + 5); // swap_ranges(starting address 1, ending address 1, starting address 2) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 16. reverse_copy - reverse_copy(a, a + 10, a); // reverse_copy(starting address, ending address, starting address) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 17. rotate_copy - rotate_copy(a, a + 3, a + 10, a); // rotate_copy(starting address, new starting address, ending address, starting address) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 18. is_sorted - cout << is_sorted(a, a + 10) << endl; // is_sorted(starting address, ending address) - - // 19. is_partitioned - cout << is_partitioned(a, a + 10, [](int x) - { return x % 2 == 0; }) - << endl; // is_partitioned(starting address, ending address, condition) - - // 20. partition - partition(a, a + 10, [](int x) - { return x % 2 == 0; }); // partition(starting address, ending address, condition) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 21. stable_partition - stable_partition(a, a + 10, [](int x) - { return x % 2 == 0; }); // stable_partition(starting address, ending address, condition) - for (int i = 0; i < 10; i++) - { - cout << a[i] << " "; - } - cout << endl; - - // 22. partition_point - auto it = partition_point(a, a + 10, [](int x) - { return x % 2 == 0; }); // partition_point(starting address, ending address, condition) - cout << *it << endl; - - // 23. random_shuffle - random_shuffle(a, a + 10); // random_shuffle(starting address, ending address) - - // 24. shuffle - shuffle(a, a + 10, default_random_engine()); // shuffle(starting address, ending address, random engine) - - // 25. max - cout << max(2, 3) << endl; // max(element 1, element 2) - - // 26. min - cout << min(2, 3) << endl; // min(element 1, element 2) - - // 27. memset - int b[10]; - memset(b, 0, sizeof(b)); // memset(starting address, value to be filled, size of the array) - - // 28. is_permutation - cout << is_permutation(a, a + 10, b) << endl; // is_permutation(starting address 1, ending address 1, starting address 2) - - // 29. next - auto it = std::next(std::begin(a), 1); // get iterator to second element of array a - - // 30. prev - auto it = std::prev(std::end(a), 1); // get iterator to last element of array a - - // 31. iota - iota(a, a + 10, 1); // iota(starting address, ending address, value to be filled) - - // 32. gcd - cout << __gcd(10, 20) << endl; // __gcd(element 1, element 2)//info: __gcd is a inbuilt function for finding gcd of two numbers - - // 33. lcm - cout << lcm(10, 20) << endl; // lcm(element 1, element 2) - - return 0; -} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp deleted file mode 100644 index 02c5173aa28..00000000000 --- a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp +++ /dev/null @@ -1,192 +0,0 @@ -#include -using namespace std; -int main() -{ - string s = "Hello World"; - cout << s << endl; - - // 1. sort - sort(s.begin(), s.end()); // sort(starting address, ending address) - cout << s << endl; - - // 2. reverse - reverse(s.begin(), s.end()); // reverse(starting address, ending address) - cout << s << endl; - - // 3. min_element - cout << *min_element(s.begin(), s.end()) << endl; // min_element(starting address, ending address) - - // 4. max_element - cout << *max_element(s.begin(), s.end()) << endl; // max_element(starting address, ending address) - - // 5. accumulate - cout << accumulate(s.begin(), s.end(), 0) << endl; // accumulate(starting address, ending address, initial value)//info: accumulate() is used to find the sum of elements in a container. - - // 6. count - cout << count(s.begin(), s.end(), 'l') << endl; // count(starting address, ending address, element to be counted) //info: count() is used to count the number of occurrences of an element in a container. - - // 7. find - cout << *find(s.begin(), s.end(), 'l') << endl; // find(starting address, ending address, element to be found)//info: find() is used to find the first occurrence of an element in a container. - - // 8. binary_search - cout << binary_search(s.begin(), s.end(), 'l') << endl; // binary_search(starting address, ending address, element to be found) - - // 9. lower_bound - cout << *lower_bound(s.begin(), s.end(), 'l') << endl; // lower_bound(starting address, ending address, element to be found)//info: lower_bound() is used to find the position of the first element in a container which is not less than a specified value. - - // 10. upper_bound - cout << *upper_bound(s.begin(), s.end(), 'l') << endl; // upper_bound(starting address, ending address, element to be found) - - // 11. rotate - rotate(s.begin(), s.begin() + 3, s.end()); // rotate(starting address, new starting address, ending address) - cout << s << endl; - - // 12. next_permutation - next_permutation(s.begin(), s.end()); // next_permutation(starting address, ending address) - cout << s << endl; - - // 13. substr - cout << s.substr(3, 4) << endl; // substr(starting index, length) - - // 14. find_first_of - cout << s.find_first_of("l") << endl; // find_first_of(string to be found) - - // 15. find_last_of - cout << s.find_last_of("l") << endl; // find_last_of(string to be found) - - // 16. find_first_not_of - cout << s.find_first_not_of("l") << endl; // find_first_not_of(string to be found) - - // 17. find_last_not_of - cout << s.find_last_not_of("l") << endl; // find_last_not_of(string to be found) - - // 18. stoi - string s1 = "123"; - cout << stoi(s1) << endl; // stoi(string to be converted) - - // 19. to_string - int x = 123; - cout << to_string(x) << endl; // to_string(int to be converted) - - // 20. isdigit - cout << isdigit(s[0]) << endl; // isdigit(character to be checked) - - // 21. isalpha - cout << isalpha(s[0]) << endl; // isalpha(character to be checked) - - // 22. isalnum - cout << isalnum(s[0]) << endl; // isalnum(character to be checked) - - // 23. islower - cout << islower(s[0]) << endl; // islower(character to be checked) - - // 24. isupper - cout << isupper(s[0]) << endl; // isupper(character to be checked) - - // 25. isxdigit - cout << isxdigit(s[0]) << endl; // isxdigit(character to be checked) - - // 26. isgraph - cout << isgraph(s[0]) << endl; // isgraph(character to be checked) - - // 27. isprint - cout << isprint(s[0]) << endl; // isprint(character to be checked) - - // 28. iscntrl - cout << iscntrl(s[0]) << endl; // iscntrl(character to be checked) - - // 29. isspace - cout << isspace(s[0]) << endl; // isspace(character to be checked) - - // 30. isblank - cout << isblank(s[0]) << endl; // isblank(character to be checked) - - // 31. ispunct - cout << ispunct(s[0]) << endl; // ispunct(character to be checked) - - // 32. toupper - cout << toupper(s[0]) << endl; // toupper(character to be converted) - - // 33. tolower - cout << tolower(s[0]) << endl; // tolower(character to be converted) - - // 34. getline - getline(cin, s); // getline(input stream, string) - - // 35. stringstream - stringstream ss; - ss << s; // ss << string - ss >> s; // ss >> string - - // 36. stringstream - stringstream ss1(s); // ss(string) - - // 37. stringstream - stringstream ss2(s, ios_base::in | ios_base::out); // ss(string, mode) - - // 38. stringstream - stringstream ss3(s, ios_base::in); // ss(string, mode) - - // 39. stringstream - stringstream ss4(s, ios_base::out); // ss(string, mode) - - // 40. stringstream - stringstream ss5(ios_base::in | ios_base::out); // ss(mode) - - // 41. stringstream - stringstream ss6(ios_base::in); // ss(mode) - - // 42. stringstream - stringstream ss7(ios_base::out); // ss(mode) - - // 43. stringstream - stringstream ss8; // ss - - // 44. stringstream - stringstream ss9(ios_base::app); // ss(mode) - - // 45. stringstream - stringstream ss10(ios_base::ate); // ss(mode) - - // 46. stringstream - stringstream ss11(ios_base::binary); // ss(mode) - - // 47. stringstream - stringstream ss12(ios_base::trunc); // ss(mode) - - // 48. stringstream - stringstream ss13(ios_base::in | ios_base::out | ios_base::binary); // ss(mode) - - // 49. stringstream - stringstream ss14(ios_base::in | ios_base::out | ios_base::ate); // ss(mode) - - // 50. stringstream - stringstream ss15(ios_base::in | ios_base::out | ios_base::app); // ss(mode) - - // 51. stringstream - stringstream ss16(ios_base::in | ios_base::out | ios_base::trunc); // ss(mode) - - // 52. stringstream - stringstream ss17(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate); // ss(mode) - - // 53. stringstream - stringstream ss18(ios_base::in | ios_base::out | ios_base::binary | ios_base::app); // ss(mode) - - // 54. stringstream - stringstream ss19(ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc); // ss(mode) - - // 55. stringstream - stringstream ss20(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::app); // ss(mode) - - // 56. stringstream - stringstream ss21(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::trunc); // ss(mode) - - // 57. stringstream - stringstream ss22(ios_base::in | ios_base::out | ios_base::binary | ios_base::app | ios_base::trunc); // ss(mode) - - // 58. stringstream - stringstream ss23(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::app | ios_base::trunc); // ss(mode) - - // 59. - return 0; -} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp deleted file mode 100644 index b80e7a1429a..00000000000 --- a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_vector.cpp +++ /dev/null @@ -1,389 +0,0 @@ -#include -using namespace std; -int main() -{ - vector v5 = {11, 32, 53, 24, 65}; - // sort - cout << "sort" << endl; - sort(v5.begin(), v5.end()); // sort in ascending order - for (auto i : v5) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - sort(v5.begin(), v5.end(), greater()); // sort in descending order - for (auto i : v5) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // permutation - - vector v = {1, 2, 3, 4, 5}; - cout << "permutation" << endl; - // prev_permutation - prev_permutation(v.begin(), v.end()); // 1 2 3 5 4 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // next_permutation - next_permutation(v.begin(), v.end()); // 1 2 3 5 4 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // rotate - cout << "rotate" << endl; - rotate(v.begin(), v.begin() + 2, v.end()); // 3 5 4 1 2 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // reverse - cout << "reverse" << endl; - reverse(v.begin(), v.end()); // 2 1 4 5 3 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // accumulate - cout << "accumulate" << endl; - int sum = accumulate(v.begin(), v.end(), 0); // 15 - cout << sum << endl; - cout << "----------------------------------------------------------------------" << endl; - - // count - cout << "count" << endl; - int cnt = count(v.begin(), v.end(), 1); // 1 - cout << cnt << endl; - cout << "----------------------------------------------------------------------" << endl; - - // find - cout << "find" << endl; - auto it = find(v.begin(), v.end(), 1); // 1 - cout << *it << endl; - cout << "----------------------------------------------------------------------" << endl; - - // binary_search - cout << "binary_search" << endl; - bool present = binary_search(v.begin(), v.end(), 1); // 1 - cout << present << endl; - cout << "----------------------------------------------------------------------" << endl; - - // lower_bound - cout << "lower_bound" << endl; - auto it = lower_bound(v.begin(), v.end(), 1); // 1 - cout << *it << endl; - cout << "----------------------------------------------------------------------" << endl; - - // upper_bound - cout << "upper_bound" << endl; - auto it = upper_bound(v.begin(), v.end(), 1); // 1 - cout << *it << endl; - cout << "----------------------------------------------------------------------" << endl; - - // max_element - cout << "max_element" << endl; - auto it = max_element(v.begin(), v.end()); // 5 - cout << *it << endl; - cout << "----------------------------------------------------------------------" << endl; - - // min_element - cout << "min_element" << endl; - auto it = min_element(v.begin(), v.end()); // 1 - cout << *it << endl; - cout << "----------------------------------------------------------------------" << endl; - - // minmax_element - cout << "minmax_element" << endl; - auto it = minmax_element(v.begin(), v.end()); // 1 5 - auto it1 = v.begin(); - auto it2 = v.end() - 1; - cout << *it1 << " " << *it2 << endl; - cout << "----------------------------------------------------------------------" << endl; - - // is_sorted - cout << "is_sorted" << endl; - bool sorted = is_sorted(v.begin(), v.end()); // 0 - cout << sorted << endl; - cout << "----------------------------------------------------------------------" << endl; - - // is_sorted_until - cout << "is_sorted_until" << endl; - auto it = is_sorted_until(v.begin(), v.end()); // 1 2 - cout << *it << endl; - cout << "----------------------------------------------------------------------" << endl; - - // is_permutation - cout << "is_permutation" << endl; - vector v1 = {1, 2, 3, 4, 5}; - vector v2 = {1, 2, 3, 4, 5}; - bool permutation = is_permutation(v1.begin(), v1.end(), v2.begin()); // 1 - cout << permutation << endl; - cout << "----------------------------------------------------------------------" << endl; - - // next_permutation - cout << "next_permutation" << endl; - next_permutation(v.begin(), v.end()); // 1 2 3 5 4 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // prev_permutation - cout << "prev_permutation" << endl; - prev_permutation(v.begin(), v.end()); // 1 2 3 4 5 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // swap - cout << "swap" << endl; - swap(v[0], v[1]); // 2 1 3 4 5 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // swap_ranges - cout << "swap_ranges" << endl; - swap_ranges(v.begin(), v.begin() + 2, v.end()); // 3 4 5 1 2 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // transform - cout << "transform" << endl; - transform(v.begin(), v.end(), v.begin(), [](int x) - { return x * x; }); // 9 16 25 1 4 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // fill - cout << "fill" << endl; - fill(v.begin(), v.end(), 5); // 5 5 5 5 5 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // remove - cout << "remove" << endl; - remove(v.begin(), v.end(), 5); // 5 5 5 5 5 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // remove_if - cout << "remove_if" << endl; - remove_if(v.begin(), v.end(), [](int x) - { return x % 2 == 0; }); // 5 5 5 5 5 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // remove_copy - cout << "remove_copy" << endl; - vector v1(5); - remove_copy(v.begin(), v.end(), v1.begin(), 5); // 5 5 5 5 5 - for (auto i : v1) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // remove_copy_if - cout << "remove_copy_if" << endl; - vector v1(5); - remove_copy_if(v.begin(), v.end(), v1.begin(), [](int x) - { return x % 2 == 0; }); // 5 5 5 5 5 - for (auto i : v1) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // replace - cout << "replace" << endl; - replace(v.begin(), v.end(), 5, 1); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // replace_if - cout << "replace_if" << endl; - replace_if( - v.begin(), v.end(), [](int x) - { return x % 2 == 0; }, - 1); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // replace_copy - cout << "replace_copy" << endl; - vector v1(5); - replace_copy(v.begin(), v.end(), v1.begin(), 5, 1); // 1 1 1 1 1 - for (auto i : v1) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // replace_copy_if - cout << "replace_copy_if" << endl; - vector v1(5); - replace_copy_if( - v.begin(), v.end(), v1.begin(), [](int x) - { return x % 2 == 0; }, - 1); // 1 1 1 1 1 - for (auto i : v1) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // reverse - cout << "reverse" << endl; - reverse(v.begin(), v.end()); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // reverse_copy - cout << "reverse_copy" << endl; - vector v1(5); - reverse_copy(v.begin(), v.end(), v1.begin()); // 1 1 1 1 1 - for (auto i : v1) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // rotate_copy - cout << "rotate_copy" << endl; - vector v1(5); - rotate_copy(v.begin(), v.begin() + 2, v.end(), v1.begin()); // 1 1 1 1 1 - for (auto i : v1) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // random_shuffle - cout << "random_shuffle" << endl; - random_shuffle(v.begin(), v.end()); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // shuffle - cout << "shuffle" << endl; - shuffle(v.begin(), v.end(), default_random_engine()); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // unique - cout << "unique" << endl; - unique(v.begin(), v.end()); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // unique_copy - cout << "unique_copy" << endl; - vector v1(5); - unique_copy(v.begin(), v.end(), v1.begin()); // 1 1 1 1 1 - for (auto i : v1) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // is_partitioned - cout << "is_partitioned" << endl; - cout << is_partitioned(v.begin(), v.end(), [](int x) - { return x % 2 == 0; }) - << endl; // 1 1 1 1 1 - cout << "----------------------------------------------------------------------" << endl; - - // partition - cout << "partition" << endl; - partition(v.begin(), v.end(), [](int x) - { return x % 2 == 0; }); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // partition_copy - cout << "partition_copy" << endl; - vector v1(5), v2(5); - partition_copy(v.begin(), v.end(), v1.begin(), v2.begin(), [](int x) - { return x % 2 == 0; }); // 1 1 1 1 1 - for (auto i : v1) - cout << i << " "; - cout << endl; - for (auto i : v2) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // stable_partition - cout << "stable_partition" << endl; - stable_partition(v.begin(), v.end(), [](int x) - { return x % 2 == 0; }); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // partition_point - - cout << "partition_point" << endl; - cout << *partition_point(v.begin(), v.end(), [](int x) - { return x % 2 == 0; }) - << endl; // 1 1 1 1 1 - cout << "----------------------------------------------------------------------" << endl; - - // stable_sort - cout << "stable_sort" << endl; - stable_sort(v.begin(), v.end()); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // partial_sort - cout << "partial_sort" << endl; - partial_sort(v.begin(), v.begin() + 2, v.end()); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // nth_element - cout << "nth_element" << endl; - nth_element(v.begin(), v.begin() + 2, v.end()); // 1 1 1 1 1 - for (auto i : v) - cout << i << " "; - cout << endl; - cout << "----------------------------------------------------------------------" << endl; - - // is_present - cout << "is_present" << endl; - cout << binary_search(v.begin(), v.end(), 3) << endl; // 1 1 1 1 1 - cout << "----------------------------------------------------------------------" << endl; - - return 0; -} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp b/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp deleted file mode 100644 index 0cdb8920a60..00000000000 --- a/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -using namespace std; - -int main() -{ - set s; - s.insert(10); - s.insert(5); - s.insert(20); - - // 1. sort - for (auto it = s.begin(); it != s.end(); it++) - { - cout << *it << " "; - } - - cout << endl; - - // 2. reverse - for (auto it = s.rbegin(); it != s.rend(); it++) - { - cout << *it << " "; - } - - cout << endl; - - // 3. min_element - cout << *min_element(s.begin(), s.end()) << endl; - - // 4. max_element - cout << *max_element(s.begin(), s.end()) << endl; - - // 5. accumulate - cout << accumulate(s.begin(), s.end(), 0) << endl; - - // 6. count - cout << count(s.begin(), s.end(), 10) << endl; - - // 7. find - cout << *find(s.begin(), s.end(), 10) << endl; - - // 8. binary_search - cout << binary_search(s.begin(), s.end(), 10) << endl; - - // 9. lower_bound - cout << *lower_bound(s.begin(), s.end(), 10) << endl; - - // 10. upper_bound - cout << *upper_bound(s.begin(), s.end(), 10) << endl; - - // // 11. rotate - // rotate(s.begin(), next(s.begin()), s.end()); - - // for (auto it = s.begin(); it != s.end(); it++) - // { - // cout << *it << " "; - // } - - // cout << endl; - - // // 12. next_permutation - // next_permutation(s.begin(), s.end()); - // for (auto it = s.begin(); it != s.end(); it++) - // { - // cout << *it << " "; - // } - - // cout << endl; - - // // 13. prev_permutation - // prev_permutation(s.begin(), s.end()); - // for (auto it = s.begin(); it != s.end(); it++) - // { - // cout << *it << " "; - // } - - return 0; -} \ No newline at end of file From e1676ff01b0009b2f1e5482b684a8bc5ff3d0f34 Mon Sep 17 00:00:00 2001 From: PRATHAMESH CHOUGALE <106848418+Prathamesh-chougale-17@users.noreply.github.com> Date: Tue, 31 Oct 2023 16:03:22 +0530 Subject: [PATCH 3/3] Adding important stl functions and properties --- .../Important_stl_and_function_array.cpp | 169 +++++++++++++++ .../Important_stl_and_function_string.cpp | 192 ++++++++++++++++++ .../important_function_of_sets.cpp | 78 +++++++ 3 files changed, 439 insertions(+) create mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp create mode 100644 Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp create mode 100644 Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp new file mode 100644 index 00000000000..df23c742b72 --- /dev/null +++ b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_array.cpp @@ -0,0 +1,169 @@ +#include +using namespace std; +int main() +{ + int a[10] = {11, 23, 53, 64, 75, 16, 27, 88, 69, 10}; // array of size 10 + + // 1. sort + sort(a, a + 10); // sort(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 2. reverse + reverse(a, a + 10); // reverse(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 3. min_element + cout << *min_element(a, a + 10) << endl; // min_element(starting address, ending address) + + // 4. max_element + cout << *max_element(a, a + 10) << endl; // max_element(starting address, ending address) + + // 5. accumulate + cout << accumulate(a, a + 10, 0) << endl; // accumulate(starting address, ending address, initial value) + + // 6. count + cout << count(a, a + 10, 10) << endl; // count(starting address, ending address, element to be counted) + + // 7. find + cout << *find(a, a + 10, 10) << endl; // find(starting address, ending address, element to be found) + + // 8. binary_search + cout << binary_search(a, a + 10, 10) << endl; // binary_search(starting address, ending address, element to be found) + + // 9. lower_bound + cout << *lower_bound(a, a + 10, 10) << endl; // lower_bound(starting address, ending address, element to be found) + + // 10. upper_bound + cout << *upper_bound(a, a + 10, 10) << endl; // upper_bound(starting address, ending address, element to be found) + + // 11. rotate + rotate(a, a + 3, a + 10); // rotate(starting address, new starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 12. next_permutation + next_permutation(a, a + 10); // next_permutation(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 13. prev_permutation + prev_permutation(a, a + 10); // prev_permutation(starting address, ending address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 14. swap + swap(a[0], a[9]); // swap(element 1, element 2) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 15. swap_ranges + swap_ranges(a, a + 5, a + 5); // swap_ranges(starting address 1, ending address 1, starting address 2) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 16. reverse_copy + reverse_copy(a, a + 10, a); // reverse_copy(starting address, ending address, starting address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 17. rotate_copy + rotate_copy(a, a + 3, a + 10, a); // rotate_copy(starting address, new starting address, ending address, starting address) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 18. is_sorted + cout << is_sorted(a, a + 10) << endl; // is_sorted(starting address, ending address) + + // 19. is_partitioned + cout << is_partitioned(a, a + 10, [](int x) + { return x % 2 == 0; }) + << endl; // is_partitioned(starting address, ending address, condition) + + // 20. partition + partition(a, a + 10, [](int x) + { return x % 2 == 0; }); // partition(starting address, ending address, condition) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 21. stable_partition + stable_partition(a, a + 10, [](int x) + { return x % 2 == 0; }); // stable_partition(starting address, ending address, condition) + for (int i = 0; i < 10; i++) + { + cout << a[i] << " "; + } + cout << endl; + + // 22. partition_point + // auto it = partition_point(a, a + 10, [](int x) + // { return x % 2 == 0; }); // partition_point(starting address, ending address, condition) + // cout << *it << endl; + + // 23. random_shuffle + random_shuffle(a, a + 10); // random_shuffle(starting address, ending address) + + // 24. shuffle + shuffle(a, a + 10, default_random_engine()); // shuffle(starting address, ending address, random engine) + + // 25. max + cout << max(2, 3) << endl; // max(element 1, element 2) + + // 26. min + cout << min(2, 3) << endl; // min(element 1, element 2) + + // 27. memset + int b[10]; + memset(b, 0, sizeof(b)); // memset(starting address, value to be filled, size of the array) + + // 28. is_permutation + cout << is_permutation(a, a + 10, b) << endl; // is_permutation(starting address 1, ending address 1, starting address 2) + + // 29. next + // auto it = std::next(std::begin(a), 1); // get iterator to second element of array a + + // 30. prev + // auto it = std::prev(std::end(a), 1); // get iterator to last element of array a + + // 31. iota + iota(a, a + 10, 1); // iota(starting address, ending address, value to be filled) + + // 32. gcd + cout << __gcd(10, 20) << endl; // __gcd(element 1, element 2)//info: __gcd is a inbuilt function for finding gcd of two numbers + + // 33. lcm + // cout << lcm(10, 20) << endl; // lcm(element 1, element 2) + + return 0; +} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp new file mode 100644 index 00000000000..02c5173aa28 --- /dev/null +++ b/Add Code Here/C++/Important_STL_properties/Important_stl_and_function_string.cpp @@ -0,0 +1,192 @@ +#include +using namespace std; +int main() +{ + string s = "Hello World"; + cout << s << endl; + + // 1. sort + sort(s.begin(), s.end()); // sort(starting address, ending address) + cout << s << endl; + + // 2. reverse + reverse(s.begin(), s.end()); // reverse(starting address, ending address) + cout << s << endl; + + // 3. min_element + cout << *min_element(s.begin(), s.end()) << endl; // min_element(starting address, ending address) + + // 4. max_element + cout << *max_element(s.begin(), s.end()) << endl; // max_element(starting address, ending address) + + // 5. accumulate + cout << accumulate(s.begin(), s.end(), 0) << endl; // accumulate(starting address, ending address, initial value)//info: accumulate() is used to find the sum of elements in a container. + + // 6. count + cout << count(s.begin(), s.end(), 'l') << endl; // count(starting address, ending address, element to be counted) //info: count() is used to count the number of occurrences of an element in a container. + + // 7. find + cout << *find(s.begin(), s.end(), 'l') << endl; // find(starting address, ending address, element to be found)//info: find() is used to find the first occurrence of an element in a container. + + // 8. binary_search + cout << binary_search(s.begin(), s.end(), 'l') << endl; // binary_search(starting address, ending address, element to be found) + + // 9. lower_bound + cout << *lower_bound(s.begin(), s.end(), 'l') << endl; // lower_bound(starting address, ending address, element to be found)//info: lower_bound() is used to find the position of the first element in a container which is not less than a specified value. + + // 10. upper_bound + cout << *upper_bound(s.begin(), s.end(), 'l') << endl; // upper_bound(starting address, ending address, element to be found) + + // 11. rotate + rotate(s.begin(), s.begin() + 3, s.end()); // rotate(starting address, new starting address, ending address) + cout << s << endl; + + // 12. next_permutation + next_permutation(s.begin(), s.end()); // next_permutation(starting address, ending address) + cout << s << endl; + + // 13. substr + cout << s.substr(3, 4) << endl; // substr(starting index, length) + + // 14. find_first_of + cout << s.find_first_of("l") << endl; // find_first_of(string to be found) + + // 15. find_last_of + cout << s.find_last_of("l") << endl; // find_last_of(string to be found) + + // 16. find_first_not_of + cout << s.find_first_not_of("l") << endl; // find_first_not_of(string to be found) + + // 17. find_last_not_of + cout << s.find_last_not_of("l") << endl; // find_last_not_of(string to be found) + + // 18. stoi + string s1 = "123"; + cout << stoi(s1) << endl; // stoi(string to be converted) + + // 19. to_string + int x = 123; + cout << to_string(x) << endl; // to_string(int to be converted) + + // 20. isdigit + cout << isdigit(s[0]) << endl; // isdigit(character to be checked) + + // 21. isalpha + cout << isalpha(s[0]) << endl; // isalpha(character to be checked) + + // 22. isalnum + cout << isalnum(s[0]) << endl; // isalnum(character to be checked) + + // 23. islower + cout << islower(s[0]) << endl; // islower(character to be checked) + + // 24. isupper + cout << isupper(s[0]) << endl; // isupper(character to be checked) + + // 25. isxdigit + cout << isxdigit(s[0]) << endl; // isxdigit(character to be checked) + + // 26. isgraph + cout << isgraph(s[0]) << endl; // isgraph(character to be checked) + + // 27. isprint + cout << isprint(s[0]) << endl; // isprint(character to be checked) + + // 28. iscntrl + cout << iscntrl(s[0]) << endl; // iscntrl(character to be checked) + + // 29. isspace + cout << isspace(s[0]) << endl; // isspace(character to be checked) + + // 30. isblank + cout << isblank(s[0]) << endl; // isblank(character to be checked) + + // 31. ispunct + cout << ispunct(s[0]) << endl; // ispunct(character to be checked) + + // 32. toupper + cout << toupper(s[0]) << endl; // toupper(character to be converted) + + // 33. tolower + cout << tolower(s[0]) << endl; // tolower(character to be converted) + + // 34. getline + getline(cin, s); // getline(input stream, string) + + // 35. stringstream + stringstream ss; + ss << s; // ss << string + ss >> s; // ss >> string + + // 36. stringstream + stringstream ss1(s); // ss(string) + + // 37. stringstream + stringstream ss2(s, ios_base::in | ios_base::out); // ss(string, mode) + + // 38. stringstream + stringstream ss3(s, ios_base::in); // ss(string, mode) + + // 39. stringstream + stringstream ss4(s, ios_base::out); // ss(string, mode) + + // 40. stringstream + stringstream ss5(ios_base::in | ios_base::out); // ss(mode) + + // 41. stringstream + stringstream ss6(ios_base::in); // ss(mode) + + // 42. stringstream + stringstream ss7(ios_base::out); // ss(mode) + + // 43. stringstream + stringstream ss8; // ss + + // 44. stringstream + stringstream ss9(ios_base::app); // ss(mode) + + // 45. stringstream + stringstream ss10(ios_base::ate); // ss(mode) + + // 46. stringstream + stringstream ss11(ios_base::binary); // ss(mode) + + // 47. stringstream + stringstream ss12(ios_base::trunc); // ss(mode) + + // 48. stringstream + stringstream ss13(ios_base::in | ios_base::out | ios_base::binary); // ss(mode) + + // 49. stringstream + stringstream ss14(ios_base::in | ios_base::out | ios_base::ate); // ss(mode) + + // 50. stringstream + stringstream ss15(ios_base::in | ios_base::out | ios_base::app); // ss(mode) + + // 51. stringstream + stringstream ss16(ios_base::in | ios_base::out | ios_base::trunc); // ss(mode) + + // 52. stringstream + stringstream ss17(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate); // ss(mode) + + // 53. stringstream + stringstream ss18(ios_base::in | ios_base::out | ios_base::binary | ios_base::app); // ss(mode) + + // 54. stringstream + stringstream ss19(ios_base::in | ios_base::out | ios_base::binary | ios_base::trunc); // ss(mode) + + // 55. stringstream + stringstream ss20(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::app); // ss(mode) + + // 56. stringstream + stringstream ss21(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::trunc); // ss(mode) + + // 57. stringstream + stringstream ss22(ios_base::in | ios_base::out | ios_base::binary | ios_base::app | ios_base::trunc); // ss(mode) + + // 58. stringstream + stringstream ss23(ios_base::in | ios_base::out | ios_base::binary | ios_base::ate | ios_base::app | ios_base::trunc); // ss(mode) + + // 59. + return 0; +} \ No newline at end of file diff --git a/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp b/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp new file mode 100644 index 00000000000..0cdb8920a60 --- /dev/null +++ b/Add Code Here/C++/Important_STL_properties/important_function_of_sets.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +int main() +{ + set s; + s.insert(10); + s.insert(5); + s.insert(20); + + // 1. sort + for (auto it = s.begin(); it != s.end(); it++) + { + cout << *it << " "; + } + + cout << endl; + + // 2. reverse + for (auto it = s.rbegin(); it != s.rend(); it++) + { + cout << *it << " "; + } + + cout << endl; + + // 3. min_element + cout << *min_element(s.begin(), s.end()) << endl; + + // 4. max_element + cout << *max_element(s.begin(), s.end()) << endl; + + // 5. accumulate + cout << accumulate(s.begin(), s.end(), 0) << endl; + + // 6. count + cout << count(s.begin(), s.end(), 10) << endl; + + // 7. find + cout << *find(s.begin(), s.end(), 10) << endl; + + // 8. binary_search + cout << binary_search(s.begin(), s.end(), 10) << endl; + + // 9. lower_bound + cout << *lower_bound(s.begin(), s.end(), 10) << endl; + + // 10. upper_bound + cout << *upper_bound(s.begin(), s.end(), 10) << endl; + + // // 11. rotate + // rotate(s.begin(), next(s.begin()), s.end()); + + // for (auto it = s.begin(); it != s.end(); it++) + // { + // cout << *it << " "; + // } + + // cout << endl; + + // // 12. next_permutation + // next_permutation(s.begin(), s.end()); + // for (auto it = s.begin(); it != s.end(); it++) + // { + // cout << *it << " "; + // } + + // cout << endl; + + // // 13. prev_permutation + // prev_permutation(s.begin(), s.end()); + // for (auto it = s.begin(); it != s.end(); it++) + // { + // cout << *it << " "; + // } + + return 0; +} \ No newline at end of file