stl

Populate a vector with all multimap values with a given key

感情迁移 提交于 2020-01-01 08:42:53
问题 Given a multimap<A,B> M what's a neat way to create a vector<B> of all values in M with a specific key. e.g given a multimap how can I get a vector of all strings mapped to the value 123? An answer is easy, looping from lower->upper bound, but is there a neat loop-free method? 回答1: Here's the way to do it STL style : // The following define is needed for select2nd with DinkumWare STL under VC++ #define _HAS_TRADITIONAL_STL 1 #include <algorithm> #include <vector> #include <map> #include

How to keep only duplicates efficiently?

ⅰ亾dé卋堺 提交于 2020-01-01 08:34:12
问题 Given an STL vector, output only the duplicates in sorted order, e.g., INPUT : { 4, 4, 1, 2, 3, 2, 3 } OUTPUT: { 2, 3, 4 } The algorithm is trivial, but the goal is to make it as efficient as std::unique(). My naive implementation modifies the container in-place: My naive implementation: void not_unique(vector<int>* pv) { if (!pv) return; // Sort (in-place) so we can find duplicates in linear time sort(pv->begin(), pv->end()); vector<int>::iterator it_start = pv->begin(); while (it_start !=

Why code using local struct as parameter for STL function does not compile in g++?

拟墨画扇 提交于 2020-01-01 08:33:30
问题 I have such code which works well: #include <algorithm> #include <iostream> char x[11]= "ABCDEFGHIJ"; char y[11]; struct F { char operator () (char c) const { return c+1; } }; int main() { std::transform(x, x+10, y, F()); y[10] = 0; std::cout <<y <<std::endl; } But if I change it to this style: #include <algorithm> #include <iostream> char x[11]= "ABCDEFGHIJ"; char y[11]; int main() { struct F { char operator () (char c) const { return c+1; } }; std::transform(x, x+10, y, F()); y[10] = 0; std

Why code using local struct as parameter for STL function does not compile in g++?

旧城冷巷雨未停 提交于 2020-01-01 08:33:00
问题 I have such code which works well: #include <algorithm> #include <iostream> char x[11]= "ABCDEFGHIJ"; char y[11]; struct F { char operator () (char c) const { return c+1; } }; int main() { std::transform(x, x+10, y, F()); y[10] = 0; std::cout <<y <<std::endl; } But if I change it to this style: #include <algorithm> #include <iostream> char x[11]= "ABCDEFGHIJ"; char y[11]; int main() { struct F { char operator () (char c) const { return c+1; } }; std::transform(x, x+10, y, F()); y[10] = 0; std

std::map Requirements for Keys (Design Decision)

半世苍凉 提交于 2020-01-01 08:25:31
问题 When I make a std::map<my_data_type, mapped_value> , what C++ expects from me is that my_data_type has its own operator< . struct my_data_type { my_data_type(int i) : my_i(i) { } bool operator<(const my_data_type& other) const { return my_i < other.my_i; } int my_i; }; The reason is that you can derive operator> and operator== from operator< . b < a implies a > b , so there's operator> . !(a < b) && !(b < a) means that a is neither less than b nor greater than it, so they must be equal. The

C++11 cmath functions not in std namespace for android NDK w/gcc-4.8 or clang 3.4

本秂侑毒 提交于 2020-01-01 08:22:34
问题 After C++11, various cmath functions previously in the global namespace are moved to the std namespace, when including the <cmath> header. However the android NDK build has issues with this, with both gcc-4.8 and clang-3.4 toolchains. The C++11 flag is correctly specified, as other c++11 particulars like unique_ptr work fine. If i attempt to use std::round , or std::cbrt , the compiler says these don't exist in std:: namespace, which they should [1]. They exist in the global namespace, but I

Filling a vector of pairs

最后都变了- 提交于 2020-01-01 08:13:07
问题 I want to fill a vector with 8 pairs. Each pair represents the moves in x and y coordinates a knight in a game of chess can make. At the moment I'm doing it like this vector<pair<int,int>> moves; pair<int,int> aPair; aPair.first = -2; aPair.second = -1; moves.push_back(aPair); aPair.first = -2; aPair.second = 1; moves.push_back(aPair); aPair.first = -1; aPair.second = -2; moves.push_back(aPair); aPair.first = -1; aPair.second = 2; moves.push_back(aPair); aPair.first = 1; aPair.second = -2;

Alternative STL implementations in C++11 and beyond

谁都会走 提交于 2020-01-01 08:05:14
问题 Over time various alternative implementations of the STL* have appeared - such as STLPort. Certain large corporations also use their own internal port of the STL for various purposes. With C++03, it's possible to write a port of the STL using only portable C++ language features, meaning that any conforming compiler should be able to compile it. But with C++11, aren't there certain features which require compiler support? For example, I don't see how std::is_standard_layout could be

The difference between front() and begin()

微笑、不失礼 提交于 2020-01-01 07:38:20
问题 What is the difference between the front() and begin() functions that appears in many STL containers? 回答1: begin() returns an iterator that can be used to iterate through the collection, while front() just returns a reference to the first element of the collection. 回答2: front() returns a reference to the first element, begin() returns an iterator to it. Note that you shouldn't call front on an empty container, but it's OK to call begin as long as you don't dereference the iterator that begin

std::vector-like class optimized to hold a small number of items [duplicate]

别说谁变了你拦得住时间么 提交于 2020-01-01 07:35:34
问题 This question already has answers here : small string optimization for vector? (4 answers) Closed 4 years ago . In one time-critical part of the program there is a member of the class that looks like that: std::vector m_vLinks; During profiling I noticed that about 99.98% of executions this vector holds only 0 or 1 items. However in very rarely cases it might hold more. This vector is definitely a bottleneck according to profiler, so I'm thinking about following optimization: Craft a hand