stdvector

Java 8 times faster with arrays than std::vector in C++. What did I do wrong?

◇◆丶佛笑我妖孽 提交于 2019-11-28 16:09:41
问题 I have the following Java code with several big arrays which never change their size. It runs in 1100 ms on my computer. I implemented the same code in C++ and used std::vector . The time of the C++ implementation which runs the exact same code is 8800 ms on my computer. What did I do wrong, so that it runs this slowly? Basically the code does the following: for (int i = 0; i < numberOfCells; ++i) { h[i] = h[i] + 1; floodedCells[i] = !floodedCells[i]; floodedCellsTimeInterval[i] =

Howto call an unmanaged C++ function with a std::vector as parameter from C#?

拟墨画扇 提交于 2019-11-28 14:16:56
I have a C# front end and a C++ backend for performance reasons. Now I would like to call a C++ function like for example: void findNeighbors(Point p, std::vector<Point> &neighbors, double maxDist); What I'd like to have is a C# wrapper function like: List<Point> FindNeigbors(Point p, double maxDist); I could pass a flat array like Point[] to the unmanaged C++ dll, but the problem is, that I don't know how much memory to allocate, because I don't know the number of elements the function will return... Is there an elegant way to handle this without having troubles with memory leaks? Thanks for

Getting a bool reference from std::vector<bool>

送分小仙女□ 提交于 2019-11-28 14:08:31
I know it's a bad habit, but I'd like to know some workaround or hack for this problem. I have a class like this: template <class T> class A : std::vector<T> { T& operator()(int index) { // returns a _reference_ to an object return this->operator[](index); } }; It's possible to do things like this: A<int> a{1,2,3,4}; a(3) = 10; But it stops working if somebody uses bool as a template parameter A<bool> a{true, false, true}; std::cout << a(0) << std::endl; // not possible if (a(1)) { /* something */ } // not possible std::vector<bool> is a specialized version of vector ( http://www.cplusplus.com

Are std::map and std::vector thread safe?

一个人想着一个人 提交于 2019-11-28 13:41:16
I am developing a multi threaded application, each thread will read (there will be no modifying of structures) from a group of maps and vectors. Can anyone please advise, since the threads are only reading from these structures would it be necessary to implement a sharable mutex around the code blocks where these structures are being read? alexrider In case of read only map/vector there is no need to use mutexes. This was already answered for both vector and map While C++03 doesn't mention threads, C++11 has clause covering you question. 23.2.2 Container data races [container.requirements

C++ reference changes when push_back new element to std::vector

半世苍凉 提交于 2019-11-28 09:26:54
I am not sure what to make of this - please tell me what's wrong with the code below. I modified my code to reduce it to the simplest terms. There is a std::vector with a bunch of MyNode objects. The first step is to get a constant reference to one of the data elements of one of these nodes (Data m_data) - in the example below, there is only one node before the 2nd node is inserted as seen below: const cv::Data& currData = m_nodesVector[currIndex].GetData(); MyNode node(...); m_nodesVector.push_back(node); At exactly the vector::push_back call, the value of currData changes!! I just don't get

C++: vector<string> *args = new vector<string>(); causes SIGABRT

纵然是瞬间 提交于 2019-11-28 08:54:52
问题 Pretty self explanatory. Here's the method that's causing the SIGABRT on the 'new vector' line: vector<string> * Task::arguments() { vector<string> *args = new vector<string>(); // CAUSES SIGABRT int count = sizeof(_arguments); for (int x = 0; x < count; x++) { string argument(_arguments[x]); args->push_back(argument); } return args; } Note that elsewhere I call that exact line without any issues. Here is the list of includes in the Task class: #include <vector> #include <unistd.h> #include

What is better: reserve vector capacity, preallocate to size or push back in loop?

馋奶兔 提交于 2019-11-28 08:41:20
I have a function that takes a pointer to char array and segment size as input arguments and calls another function that requires a std::array<std::string> . The idea is that the input char array is "sectioned" into equal parts, and string array formed. The input char array format is several smaller arrays (or strings) of determined size, concatenated togeather. These are not assumed zero-terminated, although they might be. Examples for segment size 5 and number of elements 10: char k[] = "1234\0001234\0001234\0001234\0001234\0001234\0001234\0001234\0001234\0001234\000"; char m[] = "1234

compiler error with C++ std::vector of array

。_饼干妹妹 提交于 2019-11-28 08:08:30
问题 the following code doesn't compile with gcc 4.7.0 (using std=c++11 -O3) int n; std::vector< int[4] > A; A.resize(n); the error message is length, but eventually functional cast to array type ‘_ValueType {aka int[4]}‘ Is this correct? or should this compile? And more importantly, how to avoid this problem? (without defining a new struct to hold the int[4] ) EDIT: how to solve the problem with C++98? 回答1: You cannot store arrays in a vector or any other container. The type of the elements to be

STL vector: Moving all elements of a vector

我的梦境 提交于 2019-11-28 05:41:22
I have two STL vectors A and B and I'd like to clear all elements of A and move all elements of B to A and then clear out B . Simply put, I want to do this: std::vector<MyClass> A; std::vector<MyClass> B; .... A = B; B.clear(); Since B could be pretty long, it takes k*O(N) to do this operation, where k is a constant, and N is max(size_of(A), size_of(B)) . I was wondering if there could be a more efficient way to do so. One thing that I could think of is to define A and B as pointers and then copy pointers in constant time and clear out B . Using C++11, it's as simple as: A = std::move(B); Now

In C++ check if std::vector<string> contains a certain value [duplicate]

别等时光非礼了梦想. 提交于 2019-11-28 04:49:08
This question already has an answer here: How to find out if an item is present in a std::vector? 18 answers Is there any built in function which tells me that my vector contains a certain element or not e.g. std::vector<string> v; v.push_back("abc"); v.push_back("xyz"); if (v.contains("abc")) // I am looking for one such feature, is there any // such function or i need to loop through whole vector? You can use std::find as follows: if (std::find(v.begin(), v.end(), "abc") != v.end()) { // Element in vector. } To be able to use std::find : include <algorithm> . If your container only contains