stl

Efficiency of output parameter vs return value on the stack for stl data structures

非 Y 不嫁゛ 提交于 2019-12-23 08:59:54
问题 lets say I have the functions std::Vector<Point> calculate() { std::Vector<Point> points; //do stuff with points return points; } and void calculate(std::Vector<Point>& points) { //do stuff with points } So my question is specific to objects initialized on the stack, and are stl objects. Is there any difference in performance, and what is the popular method of doing it regards 回答1: Taking the value as a reference parameter has the following properties: No copying, moving, or any other

Does <algorithm> define a macro X?

点点圈 提交于 2019-12-23 08:58:18
问题 I tried to compile the code with option C++14: #define X static_cast<double>(2) #include <algorithm> // if you change the two lines, no error occurs int main() { return 0; } But I get error after X: error: expected ',' or '...' I do not get error with option C++98. With my gcc version 4.8.4, I don't get error, but with 5.4.0 I do. Is this a bug? Do I do something wrong? 回答1: I get the same error on my local g++ 5.4.0 installation. I looked at the g++ -E output, and it seems the error traces

Why isn't std::string a specialization of std::vector? [duplicate]

痴心易碎 提交于 2019-12-23 08:58:05
问题 This question already has answers here : Why are string and vector distinct types? (4 answers) Closed 4 years ago . It seems obvious to consider a string to be a vector of characters. Why then does string have its own special implementation, which seems quite different from that of the vector class? Just to illustrate the point, here are some snippets from both classes to show that the work needed is rather similar, e.g. both using an allocator to manage memory. Also having traits could be

C++进阶之三:类型安全和STL

喜你入骨 提交于 2019-12-23 08:41:34
不要动态地处理数组 这里主要有两个意思: 不用支持动态地基类的指针进行++、+n这种操作,因为它实际会按基类大小进行偏移计算,而非预期地按照子类的大小进行偏移计算; 2.尽量在接口中使用引用而非指针,原因就在于期望清楚地表面所讨论的是一个对象,而不是对象数组; 不要使用失效对象 经常容易忽略的失效对象包括: 语义失效对象:指向已删除对象的虚悬(dangling)指针 失效的迭代器:比如,在迭代器所指向的容器开始插入之后的vector<T>::iterator i 不要使用不安全的C语言的遗留函数: strcpy\sprintf 的缓冲区都没有检查范围;strncpy\snprintf 虽然检查缓冲区界限,但是到达缓冲区界限时却不添加null。因此都是不安全的 不要使用可变长参数(...) C++ 中可变长参数的缺点包括: 缺乏类型安全性。 省略号本质是告诉编译器:关闭所有的检查,从此由我接管,启动reinterpret_cast 调用者和被调用者存在紧密耦合; 类类型对象的行为没有定义; 参数的数量未知 不要使用联合重新解释表示方式 比如一个联合中有一个long 类型的成员和一个指针类型的成员,给指针赋值,然后通过long读取其地址,这样会有问题: 不同架构和编译器中sizeof(long)可能不等于sizeof (char *) 减少了代码的可读性; 不要用memcpy 复制对象

Removing from STL std::queue without destructing the removed object?

喜欢而已 提交于 2019-12-23 08:28:44
问题 All the documentation I can find on the STL containers (both queue and list) say that for any of the remove functions the removed object's destructor is called. This means that I can't use std::queue any time I want a queue that's simply a list of objects needing some operation performed on them. I want to be able to add objects to the queue when they are waiting in line for me to do something to them. Then I want to remove them from it when I've finished with them, without destroying the

sorting table in place using stl sort

笑着哭i 提交于 2019-12-23 07:51:20
问题 I have a HUGE table (about 50Gb) in (i,j,k) format (from a sparse matrix) stored as uint32_t * idx1, * idx2; float * vals; uint32_t tablesize; and I'd like to sort it in place with a given comparison function that's a function of idx1 and idx2. Can this be done using std::sort? Specifically, each nonzero entry (i,j) with value v in the sparse matrix is stored by placing i in idx1, j in idx2, and v in the corresponding entry in vals. I'd like to then sort these entries according to (i1, j1, v1

Why is std::vector::insert complexity linear (instead of being constant)?

前提是你 提交于 2019-12-23 07:50:59
问题 Lets say I was inserting p new elements at the 'i' th position in an std::vector<mytype> of size 'n'. Since items in std::vector are guaranteed to use contiguous storage locations for their elements, it seems like this would take me 4 steps to do the above: 1) Possible reallocation of the vector if we are out of space, basically doubling its size. But that is a constant time operation (albeit a very large one). 2) Next there is a memcpy of elements from index 0 through i-1 from the old vector

Does the C++ standard specify STL implementation details for the compiler?

感情迁移 提交于 2019-12-23 07:49:17
问题 While writing an answer to this question I faced an interesting situation - the question demonstrates the scenario where one would want to put a class in an STL container but fails to do so because of a missing copy constructor/move constructor/assignment operator. In this particular case the error is triggered by std::vector::resize . I made a quick snippet as a solution and saw another answer that had provided a move constructor instead of an assignment operator and copy constructor as I

Is it safe for the input iterator and output iterator in std::transform to be from the same container?

杀马特。学长 韩版系。学妹 提交于 2019-12-23 07:46:41
问题 In this post one of the answers recommends changing a std::string case this way: std::string str = "Hello World"; std::transform(str.begin(), str.end(),str.begin(), ::toupper); I've used it and it works so far in Visual Studio 2010. But is it guaranteed by the standard to always work? My concern is that I can imagine the possibility of implementations where writing to the output iterator (the third argument) could invalidate the input iterators (arguments one and two). So, in summary, is the

STL sorted set where the conditions of order may change

五迷三道 提交于 2019-12-23 07:38:37
问题 I have a C++ STL set with a custom ordering defined. The idea was that when items get added to the set, they're naturally ordered as I want them. However, what I've just realised is that the ordering predicate can change as time goes by. Presumably, the items in the set will then no longer be in order. So two questions really: Is it harmful that the items would then be out of order? Am I right in saying that the worst that can happen is that new entries may get put into the wrong place (which