stl

sort 2-d array using stl sort()

余生长醉 提交于 2021-02-10 18:48:22
问题 I have a 2-d array, containing only 0 or 1. I want to sort it in descend order on rows (no change on each column) using STL sort algorithm. but I don't know how to pass the parameter and how to write the compare function in sort(first, last, comp); like: 0 1 1 1 1 1 0 1 1 0 1 0 would be sorted like this: 1 1 0 1 1 0 1 0 0 1 1 1 my data structure is like this: int **table = 0; table = new int *[row]; for(int i=0;i<row;i++) table[i] = new int[column]; I can only write the sort function like

Saving function evaluations while using std::min_element()

限于喜欢 提交于 2021-02-10 15:38:38
问题 Suppose you are given a vector of 2D points and are expected to find the point with the least Euclidean norm. The points are provided as std::vector<point_t> points whith the following typedef std::pair<double, double> point_t . The norm can be calculated using double norm(point_t p) { return pow(p.first, 2) + pow(p.second, 2); } Writing the loop myself I would do the following: auto leastPoint = points.cend(); auto leastNorm = std::numeric_limits<double>::max(); for (auto iter = points

why sort function of STL is not working?

十年热恋 提交于 2021-02-10 12:41:03
问题 A set of numbers will be passed as input. Also the redefined relationship of the digits 0-9 in ascending order will be passed as input. Based on the redefined relationship, the set of numbers must be listed in ascending order. Sample I/O Input: 20 50 11 121 9231476058 Output: 50 11 20 121 The program which i wrote having an error about which i am not aware of. so, please help me in debugging it. Program specification: 1.) I created one adjacency list for grouping the numbers on the basis of

why sort function of STL is not working?

我只是一个虾纸丫 提交于 2021-02-10 12:40:02
问题 A set of numbers will be passed as input. Also the redefined relationship of the digits 0-9 in ascending order will be passed as input. Based on the redefined relationship, the set of numbers must be listed in ascending order. Sample I/O Input: 20 50 11 121 9231476058 Output: 50 11 20 121 The program which i wrote having an error about which i am not aware of. so, please help me in debugging it. Program specification: 1.) I created one adjacency list for grouping the numbers on the basis of

Iterating a vector in reverse order with a range-for loop? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 06:58:41
问题 This question already has answers here : C++11 reverse range-based for-loop (9 answers) Closed 10 months ago . I want to iterate a vector in reverse order , i know that i can do it easily by below code #include<bits/stdc++.h> using namespace std; int main() { vector<int> v(10); for(auto it=v.rbegin();it!=v.rend();it++) cout<<*it<<""; } For iterating in forward we can simply do by below code. for(auto it:v) cout<<it<<" "; Now task is ... Is it possible to do reverse iterate similar to for(auto

Iterating a vector in reverse order with a range-for loop? [duplicate]

只愿长相守 提交于 2021-02-10 06:58:02
问题 This question already has answers here : C++11 reverse range-based for-loop (9 answers) Closed 10 months ago . I want to iterate a vector in reverse order , i know that i can do it easily by below code #include<bits/stdc++.h> using namespace std; int main() { vector<int> v(10); for(auto it=v.rbegin();it!=v.rend();it++) cout<<*it<<""; } For iterating in forward we can simply do by below code. for(auto it:v) cout<<it<<" "; Now task is ... Is it possible to do reverse iterate similar to for(auto

stl::iterators with raw pointers

假如想象 提交于 2021-02-09 12:43:04
问题 I want to use iterators with C++ arrays, but with raw pointers too. I can do with a static vector: #define SIZE 10 int vect[SIZE] = {0}; vect[3] = 5; int* p = std::find(std::begin(vect), std::end(vect), 5); bool success = p != std::end(vect); How can be possible to do it with a raw pointer (maybe a heap allocated vector)? Of course the compiler does not know the size of the data, so this code int* pStart = vect; std::find(std::begin(pStart), std::end(pStart), 5); gives error C2784: '_Ty *std:

ref-qualifiers for the assignment operator of standard library types

本秂侑毒 提交于 2021-02-09 10:55:43
问题 I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are. Because of that, we can write things such as this: std::string{} = "42"; std::string s = "hello " + std::string{"world"} = "oops!"; std::vector<int> v = { 1,2,3 }; std::move(v) = { 4,5,6 }; If the assignment operator was lvalue ref-qualified all of these examples would not compile. Is it because there's a lot of things to modify (but then so it was for noexcept) and

ref-qualifiers for the assignment operator of standard library types

牧云@^-^@ 提交于 2021-02-09 10:54:09
问题 I was wondering, is there a reason the assignment operator of standard types is not lvalue ref-qualified? None of them are. Because of that, we can write things such as this: std::string{} = "42"; std::string s = "hello " + std::string{"world"} = "oops!"; std::vector<int> v = { 1,2,3 }; std::move(v) = { 4,5,6 }; If the assignment operator was lvalue ref-qualified all of these examples would not compile. Is it because there's a lot of things to modify (but then so it was for noexcept) and

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

两盒软妹~` 提交于 2021-02-08 20:37:07
问题 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