stdvector

C++11: Range-looping vector from the second element?

主宰稳场 提交于 2019-12-29 07:42:18
问题 I have a std::vector<std::string> v; (initialized). How can I use the range-for loop for accessing all elements except the first one (on index zero). For all elements: for (const string & s: v) process(s); Instead of the v a range expression can be used. How can I write the range expression to skip the first element (or skip the first n elements) ? I know how to get the effect using v.begin() + 1 and using the classic loop. I am searching for the new, more readable, recommended alternative to

Amortized analysis of std::vector insertion

余生长醉 提交于 2019-12-29 03:57:05
问题 How do we do the analysis of insertion at the back (push_back) in a std::vector? It's amortized time is O(1) per insertion. In particular in a video in channel9 by Stephan T Lavavej and in this ( 17:42 onwards ) he says that for optimal performance Microsoft's implementation of this method increases capacity of the vector by around 1.5. How is this constant determined? 回答1: Assuming you mean push_back and not insertion, I believe that the important part is the multiply by some constant (as

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

女生的网名这么多〃 提交于 2019-12-29 01:48:08
问题 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

Vector of std::function with different signatures

偶尔善良 提交于 2019-12-28 05:45:06
问题 I have a number of callback functions with different signatures. Ideally, I would like to put these in a vector and call the appropriate one depending on certain conditions. e.g. void func1(const std::string& value); void func2(const std::string& value, int min, int max); const std::vector<std::function<void(std::string)>> functions { func1, func2, }; I realise the above isn't possible, but I wonder if there are any alternatives I should consider. I haven't been able to find any yet, and I've

C++ valarray vs. vector

三世轮回 提交于 2019-12-27 16:32:45
问题 I like vectors a lot. They're nifty and fast. But I know this thing called a valarray exists. Why would I use a valarray instead of a vector? I know valarrays have some syntactic sugar, but other than that, when are they useful? 回答1: Valarrays (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make a valarray of pointers so the compiler can make assumptions about the code and optimise it better. (The main reason that Fortran is so fast is that there is no

How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

时光总嘲笑我的痴心妄想 提交于 2019-12-27 11:43:29
问题 I have an array of integers that I need to remove duplicates from while maintaining the order of the first occurrence of each integer. I can see doing it like this, but imagine there is a better way that makes use of STL algorithms better? The insertion is out of my control, so I cannot check for duplicates before inserting. int unsortedRemoveDuplicates(std::vector<int> &numbers) { std::set<int> uniqueNumbers; std::vector<int>::iterator allItr = numbers.begin(); std::vector<int>::iterator

how to map a Eigen::Matrix to std::vector<Eigen::vector>?

谁说胖子不能爱 提交于 2019-12-25 03:25:06
问题 E.g., if I have an Eigen::MatrixXd of size 10 columns and 3 rows, how can I alias that to a std::vector of 10 elements of Eigen::Vector3d ? when I say alias I mean using the same memory block without copying. I know that I can do the reversed mapping by something like: std::vector<Vector3d> v(10); ... Map<Matrix<double,3,Dynamic> > m(v.data().data(), 3, 10); but reversely, if I have an Eigen::Matrix, I tried to convert it to a vector of Eigen::vector but the following code line failed

Is using assign() a good way to initialise my C++ vector of structs?

怎甘沉沦 提交于 2019-12-24 17:04:27
问题 The struct struct Vanish { int iCount; int iRow; }; I defined a std::vector of Vanish as a member of my class, and want to initialise it in the constructor like this: class BigClass { public: BigClass(); private: std::vector<Vanish> tovanish; }; void BigClass::BigClass() { Vanish empty = {0,0}; tovanish.assign(MAX_VANISH, empty); } Is there a better way, or is this considered OK? 回答1: It is better to do that in the constructor's initializer list: BigClass::BigClass() : tovanish(MAX_VANISH) {

Is using assign() a good way to initialise my C++ vector of structs?

旧巷老猫 提交于 2019-12-24 17:03:10
问题 The struct struct Vanish { int iCount; int iRow; }; I defined a std::vector of Vanish as a member of my class, and want to initialise it in the constructor like this: class BigClass { public: BigClass(); private: std::vector<Vanish> tovanish; }; void BigClass::BigClass() { Vanish empty = {0,0}; tovanish.assign(MAX_VANISH, empty); } Is there a better way, or is this considered OK? 回答1: It is better to do that in the constructor's initializer list: BigClass::BigClass() : tovanish(MAX_VANISH) {

Why doesn't this vector assignment work?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 15:14:44
问题 Similar Questions: STL vector reserve() and copy() std::vector reserve() and push_back() is faster than resize() and array index, why? std::vector::resize() vs. std::vector::reserve() #include <vector> #include <iostream> using namespace std; int main() { vector<vector<int> > vvi; vvi.resize(1); vvi[0].reserve(1); vvi[0][0] = 1; vector<int> vi = vvi[0]; cout << vi[0]; // cout << vvi[0][0]; works return 0; } This gives me a seg fault, and I can't tell why. 回答1: vvi[0].reserve(1); vvi[0][0] = 1