stdvector

Alternatives to std::vector due to reallocation that invalidates pointers to elements

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-04 19:06:20
问题 this might be a newb question (i am) but i've searched as much as i could to find a solution to the following problem I have the following scenario (heavily distilled of course): class Container { std::vector<Object> obj; }; class Pointers { std::vector<Object*> obj_ptr; }; I have a routine that pushes back an element of type Object to the vector obj in Container then pushes back the pointer to that same element to obj_ptr . the overall idea is that obj_ptr[i] == &obj[i] throughout the life

Alternatives to std::vector due to reallocation that invalidates pointers to elements

不羁的心 提交于 2021-02-04 19:04:52
问题 this might be a newb question (i am) but i've searched as much as i could to find a solution to the following problem I have the following scenario (heavily distilled of course): class Container { std::vector<Object> obj; }; class Pointers { std::vector<Object*> obj_ptr; }; I have a routine that pushes back an element of type Object to the vector obj in Container then pushes back the pointer to that same element to obj_ptr . the overall idea is that obj_ptr[i] == &obj[i] throughout the life

Create an Array of vectors in C++

安稳与你 提交于 2021-01-29 05:37:49
问题 I want to create a distance matrix of a big dataset, and only want to store the 'close' enough elements. The code reads like this vector<double> * D; D = (vector<double> *) malloc(dim *sizeof(vector<double>) ) ; for(i=0;i<dim;i++){ for(j=i+1;j<dim;j++){ dx = s[j][0] - s[i][0]; dy = s[j][1] - s[i][1]; d = sqrt( dx*dx + dy*dy ); if(d < MAX_DISTANCE){ D[i].push_back(d); D[j].push_back(d); } } which gives me segmentation fault. I guess I have not defined the array of vector correctly. How do I

Copy vector of vectors in copy constructor

六月ゝ 毕业季﹏ 提交于 2021-01-29 03:13:35
问题 A simple thing as I thought at first seems to be harder than I thought. I want to copy a vector of vectors of type int inside a copy constructor. std::vector<std::vector<int> * > * bar; I tried this but it is not working: Foo(const Foo& rhs) : bar(new std::vector<std::vector<int> * >(rhs.vec->size())) { for (std::size_t i = 0; i < rhs.bar->size(); i++) { bar->push_back(new std::vector<int>()); for (size_t j = 0; j < (*rhs.bar)[i]->size(); j++) { bar->back()->push_back((*rhs.bar)[i]->at(j)); }

Undefined reference error from GCC using a template with a std::vector and an Eigen Matrix?

泪湿孤枕 提交于 2021-01-28 12:45:08
问题 I am experiencing an undefined reference error, when compiling the following code using GCC 4.7.2 20130108 under x86_64-suse-linux via the command: g++ main.cpp func.cpp -I/path/to/eigenlibrary/eigen_3.2.1 The error message reads: main.cpp:(.text+0x1d): undefined reference to `void f<2>(std::vector<Eigen::Matrix<double, 2, 2, ((Eigen::._84)0)|((((2)==(1))&&((2)!=(1)))? ((Eigen::._84)1) : ((((2)==(1))&&((2)!=(1)))?((Eigen::._84)0) : ((Eigen::._84)0))), 2, 2>, std::allocator<Eigen::Matrix

Undefined reference error from GCC using a template with a std::vector and an Eigen Matrix?

こ雲淡風輕ζ 提交于 2021-01-28 12:43:59
问题 I am experiencing an undefined reference error, when compiling the following code using GCC 4.7.2 20130108 under x86_64-suse-linux via the command: g++ main.cpp func.cpp -I/path/to/eigenlibrary/eigen_3.2.1 The error message reads: main.cpp:(.text+0x1d): undefined reference to `void f<2>(std::vector<Eigen::Matrix<double, 2, 2, ((Eigen::._84)0)|((((2)==(1))&&((2)!=(1)))? ((Eigen::._84)1) : ((((2)==(1))&&((2)!=(1)))?((Eigen::._84)0) : ((Eigen::._84)0))), 2, 2>, std::allocator<Eigen::Matrix

Which is the fastest? A boost::multi_array or a std::vector?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-28 06:51:28
问题 Which is the fastest? A boost::multi_array or a std::vector ? I will have (not constant) 17.179.869 elements stored in 3 dimensions which will need to be accessed inside a for loop very rapidly and very often. What would be the most performing? An std::vector or a boost::multi_array ? (I don't expect it to be done within a second, but I would like to have it as efficient as possible because a nanosecond difference can save a lot of time.) 回答1: Best advice is to benchmark it by yourself. In

std::out_of_range exception is not thrown

折月煮酒 提交于 2021-01-27 11:31:27
问题 // The following code works fine, throwing a std::out_of_range exception: std::vector<double> vd{ 1.5 }; try { int i{ -1 }; double d = vd.at(i); // exception is thrown } catch (std::out_of_range& re) { std::cout << "Exception is " << re.what() << std::endl; // invalid vector subscript } If I access vector elements in a for loop with an invalid index, no std::exception is thrown although I use .at() . Why is the std::out_of_range exception not thrown? // in a for loop, this does not throw the

std::out_of_range exception is not thrown

醉酒当歌 提交于 2021-01-27 11:31:06
问题 // The following code works fine, throwing a std::out_of_range exception: std::vector<double> vd{ 1.5 }; try { int i{ -1 }; double d = vd.at(i); // exception is thrown } catch (std::out_of_range& re) { std::cout << "Exception is " << re.what() << std::endl; // invalid vector subscript } If I access vector elements in a for loop with an invalid index, no std::exception is thrown although I use .at() . Why is the std::out_of_range exception not thrown? // in a for loop, this does not throw the

How to properly static cast a vector in C++?

喜欢而已 提交于 2021-01-27 07:11:26
问题 I have a code in which at the end of a function I need to cast from int to double all the elements of an array in order to being able to do a final push_back before exiting the function. The code I have right now is: template <class T, size_t dims> class A { typedef typename std::array<int, dims> ArrayInt; typedef typename std::array<double, dims> ArrayDouble; typedef typename std::vector <ArrayDouble> VectorDouble; /* ...*/ foo() { /* ...*/ ArrayInt myArrayInt; ArrayDouble myArrayDouble;