stl

Why is <algorithm> not needed for std::copy or std::swap?

故事扮演 提交于 2021-02-08 20:37:04
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

我的未来我决定 提交于 2021-02-08 20:36:56
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

你离开我真会死。 提交于 2021-02-08 20:36:43
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

故事扮演 提交于 2021-02-08 20:36:40
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

半城伤御伤魂 提交于 2021-02-08 20:36:21
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why SGI STL don't use the copy-and-swap idiom?

删除回忆录丶 提交于 2021-02-08 14:10:40
问题 I recently read an answer on StackOverflow about What is the copy-and-swap idiom? and knew that the copy-and-swap idiom can avoiding code duplication, and providing a strong exception guarantee. However, when I looked into SGI STL deque implementation, I found that it doesn't use the idiom. I'm wondering why not, if the idiom is somehow like a "best practice"? deque& operator= (const deque& __x) { const size_type __len = size(); if (&__x != this) { if (__len >= __x.size()) erase(copy(__x

Comparator for vector<pair<int,int>> [duplicate]

我只是一个虾纸丫 提交于 2021-02-08 12:13:41
问题 This question already has answers here : How do I sort a vector of pairs based on the second element of the pair? (7 answers) Closed 6 years ago . vector<pair<int,int> > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar values of v[i].first , v[i].second is sorted in decreasing order? like:- i/p: 13 10 44 15 13 15 13 99 6 45 o/p: 6

Comparator for vector<pair<int,int>> [duplicate]

泄露秘密 提交于 2021-02-08 12:12:33
问题 This question already has answers here : How do I sort a vector of pairs based on the second element of the pair? (7 answers) Closed 6 years ago . vector<pair<int,int> > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar values of v[i].first , v[i].second is sorted in decreasing order? like:- i/p: 13 10 44 15 13 15 13 99 6 45 o/p: 6

Vectors in C++ , bucket sort : Segmentation fault

﹥>﹥吖頭↗ 提交于 2021-02-08 12:07:28
问题 In my code, I am trying to implement bucket sort and in my implementation I have tried using vectors, but unfortunately I end up with errors with respect to vector functions. Code : #include <iostream> #include <algorithm> #include <vector> using namespace std; void bucket_sort(vector<float> & , int); //vector is passed by reference int main(){ vector<float> array; float element; int count; cout << "\nEnter the size of the vector : "; cin >> count; cout << "\nEnter the elements into the

Vectors in C++ , bucket sort : Segmentation fault

放肆的年华 提交于 2021-02-08 12:06:15
问题 In my code, I am trying to implement bucket sort and in my implementation I have tried using vectors, but unfortunately I end up with errors with respect to vector functions. Code : #include <iostream> #include <algorithm> #include <vector> using namespace std; void bucket_sort(vector<float> & , int); //vector is passed by reference int main(){ vector<float> array; float element; int count; cout << "\nEnter the size of the vector : "; cin >> count; cout << "\nEnter the elements into the