stl

Finding max_element of a vector where a member is used to decide if its the maximum

↘锁芯ラ 提交于 2021-02-04 15:33:18
问题 Consider a class A having a member x and a std::vector< A >. Now its a common task to search for the maximal x among all elements inside the vector. Clearly I can only use std::max_element if there is an iterator on the x's. But I must write one by my own, or I just make a simple for loop. maxSoFar = -std::numeric_limits< double >::max(); for( std::vector< A >::const_iterator cit = as.begin(); cit != as.end(); ++cit ) { if( cit->x > maxSoFar ) maxSoFar = cit->x; } but it's so tedious, and I

Efficiently moving contents of std::unordered_set to std::vector

久未见 提交于 2021-02-04 11:24:05
问题 In my code I have a std::unordered_set and I need to move the data into a std::vector . I'm using the std::unordered_set while getting the data to ensure only unique values are stored prior to converting to a std::vector . My question is how do I move the contents to the std::vector the most efficiently? I don't need the std::unordered_set after the data is moved. I currently have the following: std::copy(set.begin(), set.end(), std::back_inserter(vector)); 回答1: Before C++17, the best you can

Why does std::numeric_limits<long long>::max() fail? [duplicate]

半腔热情 提交于 2021-02-04 07:21:09
问题 This question already has answers here : #define NOMINMAX using std::min/max (4 answers) Closed 4 years ago . This line of code fails to compile in VS2015 Update 3: auto a = std::numeric_limits<long long>::max(); It cannot find the definition of max() . Why is this? 回答1: That max call may interfere with "evil" max preprocessor macro defined in the Windows SDK headers, that you have probably included (directly or indirectly). An option is to prevent the preprocessor max macro to kick in, using

Line 5: Char 54: error: no matching function for call to 'min(int, std::__cxx11::basic_string<char>::size_type)'

点点圈 提交于 2021-02-02 09:54:40
问题 class Solution { public: string reverseStr(string s, int k) { for (int start = 0; start < s.size(); start += 2 * k) { int end = min(start + k - 1, s.size() - 1); while (start < end) { swap(s[start], s[end]); start++; end--; } } return s; } }; Line 5: Char 54: error: no matching function for call to 'min(int, std::__cxx11::basic_string::size_type)' 回答1: As the compiler tries to tell you, the issue is that the types of start + k -1 and s.size() - 1 are different. So one way to fix this is to

Implementation of string::find_first_of

天涯浪子 提交于 2021-01-29 14:14:54
问题 My confusion comes from a solution for problem 792. Number of Matching Subsequences in leetcode, the naive solution is to check all characters in S for each searching word. The time complexity is too high to pass all test cases as outlined in the official answer. The first code piece listed below could beats 100% merely because it calls string::find_first_of function, while my hand written codes uses exactly the same routine(I believe) get TLE as expected. So, why the first one is so fast? //

std::ostringstream overwriting initializing string

喜夏-厌秋 提交于 2021-01-29 10:30:52
问题 The following code results in "0004567" on clang++-7 #include <iostream> #include <sstream> using namespace std; int main() { ostringstream oss{"1234567"}; oss << "000"; cout << oss.str() << endl; } Now is this correct STL implementation? I can't think of how is it useful to initialize with a string that will be overwritten... 回答1: @IgorTandetnik gave your a solution - to add std::ios_base::app std::ostringstream constructor argument. However, there is no benefit in passing the initial string

Why is an error popping out when i am trying to use priority_queue with parameters as pointer to a structure

℡╲_俬逩灬. 提交于 2021-01-29 07:56:22
问题 ## Priority Queue throws an error with pointers. When I try to use structure pointers as parameter for priority queue and use comparator function the code gives an error , but priority seems to work fine with objects. ## #include<bits/stdc++.h> using namespace std; struct data { int cost; int node; int k; data(int a,int b,int c):cost(a),node(b),k(c){}; }; struct cmp { bool operate(data *p1,data *p2) { return p1->cost<p2->cost; } }; int main () { priority_queue<data*,vector<data*>,cmp> pq; pq

What is the list::insert behavior when the informed iterator argument is initialized to the begining of an empty list?

拜拜、爱过 提交于 2021-01-28 13:50:47
问题 Suppose you have a C++ empty list: list<int> l; and you insert three new elements from the beginning: auto it = l.begin(); l.insert(it,10); l.insert(it,20); l.insert(it,30); when trying to print the list elements from the beginning to the end: for(int i: l){ cout << i << ' '; } the obtained result is: 10 20 30 . But it is supposed that insert function inserts elements before the element pointed by the iterator, so the obtained result should have been: 30 20 10 . Why does this happen? 回答1:

What is the list::insert behavior when the informed iterator argument is initialized to the begining of an empty list?

ぃ、小莉子 提交于 2021-01-28 13:46:35
问题 Suppose you have a C++ empty list: list<int> l; and you insert three new elements from the beginning: auto it = l.begin(); l.insert(it,10); l.insert(it,20); l.insert(it,30); when trying to print the list elements from the beginning to the end: for(int i: l){ cout << i << ' '; } the obtained result is: 10 20 30 . But it is supposed that insert function inserts elements before the element pointed by the iterator, so the obtained result should have been: 30 20 10 . Why does this happen? 回答1:

Eclipse/CDT Pretty Print Errors

亡梦爱人 提交于 2021-01-28 12:41:11
问题 Let me start by saying that I've scoured other questions on here that are incredibly closely related, but haven't lent themselves to resolving my issue. Setup: Win7 laptop, SSH/X11 to CentOS 6.4 Final running Eclipse Kepler SR2 w/ CDT 8.3 GDB 7.2 Managed corporate server that does not have Internet access (so no svn co... ) I am new to using Eclipse/CDT, but familiar with GDB. When I try to debug an application that uses STL, upon the first occurrence of an STL object, I get the below error(s