std

Remove element from std::map based on the time of insertion

旧时模样 提交于 2019-12-06 20:26:16
问题 I need to erase elements from an std::map based on the time of insertion (or something else more efficient than that). The map will probably hold thousands of elements and if I store the time and iterate the map to check each elements time, it will probably end up being quite time consuming. Does anyone have any good idea how to erase elements from a std::map when they are getting old? 回答1: The std::map<> type has no notion of when an element was inserted. It only serves to hold a key / value

Destroy std::vector without releasing memory

自闭症网瘾萝莉.ら 提交于 2019-12-06 20:22:36
问题 Lets say I have a function to get data into an std vector: void getData(std::vector<int> &toBeFilled) { // Push data into "toBeFilled" } Now I want to send this data to another function, that should free the data when finished: void useData(int* data) { // Do something with the data... delete[] data; } Both functions (getData and useData) are fixed and cannot be changed. This works fine when copying the data once: { std::vector<int> data; getData(data); int *heapData = new int[data.size()];

Segmentation fault in std function std::_Rb_tree_rebalance_for_erase ()

不打扰是莪最后的温柔 提交于 2019-12-06 20:17:29
(Note to any future readers: The error, unsurprisingly, is in my code and not std::_Rb_tree_rebalance_for_erase () ) I'm somewhat new to programming and am unsure how to deal with a segmentation fault that appears to be coming from a std function. I hope I'm doing something stupid (i.e., misusing a container), because I have no idea how to fix it. The precise error is Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x000000000000000c 0x00007fff8062b144 in std::_Rb_tree_rebalance_for_erase () (gdb) backtrace #0 0x00007fff8062b144 in std:

Regex error using MSVC 2013

懵懂的女人 提交于 2019-12-06 20:03:26
I have a piece of xml like code to parse, using std::regex in MSVC 2013 <GLVertex> #version 450 core layout(location = 0) in vec3 pos; in VertexInfo{ vec2 uv; }vertexInfo; void main(){ gl_Position = vec4(pos, 1.0); vertexInfo.uv = pos.xy; } <GLVertex/> <GLFragment> #version 450 core layout(location = 0) uniform sampler2D g_map; uniform Color { vec4 color; }; layout(location = 0) out vec4 fragColor; void main(){ fragColor = texture(g_map, vertexInfo.uv); } <GLFragment/> Here is the pattern: <GLVertex>((.|\n)+)<GLVertex\/> But the program always crash! Is there any bug in my regex? I've tested

Why std:: is not needed when using ispunct() in C++?

做~自己de王妃 提交于 2019-12-06 19:02:40
问题 #include <iostream> #include <string> #include <cctype> using std::string; using std::cin; using std::cout; using std::endl; int main() { string s("Hello World!!!"); decltype(s.size()) punct_cnt = 0; for (auto c : s) if (ispunct(c)) ++punct_cnt; cout << punct_cnt << " punctuation characters in " << s << endl; } It seems that I can use ispunct() without std:: or declaring using std::ispunct; but I can't do that with std::cout or std::cin . Why is this happening? 回答1: It means ispunct is part

Explicit copy constructor and std::sort

拥有回忆 提交于 2019-12-06 18:51:50
问题 When sorting a container of objects having an explicit copy ctor I get compiler errors (from g++ 4.8.2 and clang++ 3.4, both in -std=c++11 mode) that I don't understand. I've created a simple example to demonstrate the problem class A { public: explicit A(int i): m_i(i) {}; explicit A(const A& other): m_i(other.m_i) {}; int i() const {return m_i;}; private: int m_i; }; bool is_less(const A& a, const A& b) { return a.i() < b.i(); } int main(int, char*[]) { std::vector<A> objects; objects.push

Get index of object inserted into a vector

[亡魂溺海] 提交于 2019-12-06 18:08:04
问题 How can I get the position where my object was actually inserted? #include <vector> using namespace std; vector<SomeClass> list; SomeClass object; list.push_back(object); list[...].method(); // I do not have the key Unfortunately push_back does not return anything since its return type is void . 回答1: If v is your vector, the following will give you the position (that is, the index): v.push_back(object); size_t pos = v.size() - 1; Or you can look at the size() before calling push_back() . Then

Can std::function be move-constructed from rvalue reference to a temporary functor object?

你。 提交于 2019-12-06 17:06:04
问题 I have an untemplated functor object that I'm trying to store as a std::function inside another object. This object is really heavyweight, so it's marked as uncopyable, but it does have a move constructor. However, trying to construct a std::function, or assign it, from a temporary constructor fails. Here is a minimal example to provoke the error. // pretend this is a really heavyweight functor that can't be copied. struct ExampleTest { int x; int operator()(void) const {return x*2;}

Why is it better to use std::make_* instead of the constructor?

蹲街弑〆低调 提交于 2019-12-06 17:00:42
问题 There are some functions in the STL which start with the make_ prefix like std::make_pair , std::make_shared , std::make_unique etc. Why is it a better practice to use them instead of simply using the constructor ? auto pair2 = std::pair< int, double >( 1, 2.0 ); auto pair3 = std::make_pair( 1, 2.0 ); std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) ); std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 ); I just see that these functions make the code a little

std::sort and custom swap function

ε祈祈猫儿з 提交于 2019-12-06 14:00:59
I currently have an array of pair<double, int> which I sort using a simple custom comparator function e.g. // compare by first int sort_index_lcomparator(const pair<double, int>& a, const pair<double, int>& b) { return a.first < b.first; } // then sort simply like pair<double, int> arr[size]; std::sort(arr, arr + size, sort_index_lcomparator); I'm actually interested in the index order and not in the sorted doubles. My problem is that I would like to change away from this structure and have instead a struct of two arrays rather than an array of a struct i.e. I would like to optimize for