vector

Writing/reading large vectors of data to binary file in c++

谁说胖子不能爱 提交于 2021-02-07 20:51:16
问题 I have a c++ program that computes populations within a given radius by reading gridded population data from an ascii file into a large 8640x3432-element vector of doubles. Reading the ascii data into the vector takes ~30 seconds (looping over each column and each row), while the rest of the program only takes a few seconds. I was asked to speed up this process by writing the population data to a binary file, which would supposedly read in faster. The ascii data file has a few header rows

C++ - Safety of accessing element of vector via pointers

你离开我真会死。 提交于 2021-02-07 20:40:28
问题 In a C++ project of mine, I am using a vector to hold a bunch of struct s which hold a number of elements for a simple game (ie: tic-tac-toe, coordinates, x vs o , etc). ie: struct gameMove { int x; int y; int player; int order; }; Every time during a single game, whenever a player makes a move (ie: places an x or o ), the information is stored in the vector via push_back() , to create an "undo" feature, which currently works as expected. In some parts of my undo/redo logic, I am using

Choosing SSE instruction execution domains in mixed contexts

夙愿已清 提交于 2021-02-07 19:43:33
问题 I am playing with a bit of SSE assembly code in which I do not have enough xmm registers to keep all the temporary results and useful constants in registers at the same time. As a workaround, for some constant vectors that have identical components, I “compress” several vectors into a single xmm register, xmm14 below. I use the pshufd instruction to decompress the constant vector I need. This instruction has a bit of latency, but since it takes a source and a destination register, it is

Pickling a vector in boost python?

独自空忆成欢 提交于 2021-02-07 18:17:43
问题 I have this simple C++ code: class Contained {}; class CannotPickle { public: CannotPickle() {}; CannotPickle(std::vector<boost::shared_ptr<Contained>> new_vector) : my_vector(new_vector) {}; std::vector<boost::shared_ptr<Contained>> my_vector; }; struct CannotPickle_pickle_suite : boost::python::pickle_suite { static boost::python::tuple getinitargs(CannotPickle const& c) { return boost::python::make_tuple(c.my_vector); } }; I'm trying to enable pickling support for CannotPickle like this:

Traversing a vector in reverse direction with size_t values

橙三吉。 提交于 2021-02-07 18:09:52
问题 I want to traverse through the values of a vector in opposite direction. As you know the size of a vector is of size_t. When I use the following code: for(size_t r=m.size()-1; r >= 0; r--) { x[r] = f[r]; for(size_t c = r+1; c < m.size(); c++) { x[r] -= m[r][c] * x[c]; } } I will go out of the range of the vector because the r will become 4294967295 after decrementing r = 0. I am not changing the r's type because in my project, I am treating warnings as errors, so it should be size_t or I

Traversing a vector in reverse direction with size_t values

微笑、不失礼 提交于 2021-02-07 18:09:03
问题 I want to traverse through the values of a vector in opposite direction. As you know the size of a vector is of size_t. When I use the following code: for(size_t r=m.size()-1; r >= 0; r--) { x[r] = f[r]; for(size_t c = r+1; c < m.size(); c++) { x[r] -= m[r][c] * x[c]; } } I will go out of the range of the vector because the r will become 4294967295 after decrementing r = 0. I am not changing the r's type because in my project, I am treating warnings as errors, so it should be size_t or I

Traversing a vector in reverse direction with size_t values

£可爱£侵袭症+ 提交于 2021-02-07 18:08:58
问题 I want to traverse through the values of a vector in opposite direction. As you know the size of a vector is of size_t. When I use the following code: for(size_t r=m.size()-1; r >= 0; r--) { x[r] = f[r]; for(size_t c = r+1; c < m.size(); c++) { x[r] -= m[r][c] * x[c]; } } I will go out of the range of the vector because the r will become 4294967295 after decrementing r = 0. I am not changing the r's type because in my project, I am treating warnings as errors, so it should be size_t or I

Emplace back thread on vector

拥有回忆 提交于 2021-02-07 18:01:55
问题 Why this cause undefined behavior? #include <iostream> #include <thread> #include <vector> std::vector<std::thread> threads(3); void task() { std::cout<<"Alive\n";} void spawn() { for(int i=0; i<threads.size(); ++i) //threads[i] = std::thread(task); threads.emplace_back(std::thread(task)); for(int i=0; i<threads.size(); ++i) threads[i].join(); } int main() { spawn(); } If I will create threads as in commented line thread is copied/moved assignment so its fine, but why is not working when

Why can't I delete last element of vector

家住魔仙堡 提交于 2021-02-07 14:26:51
问题 I have stl vector consisting of several elements. I need to iterate through this vector and delete elements which meets some criteria. So I wrote this code for (int j = imageDataVector.size()-1; j >= 0; j--) { if(imageDataVector[i] < threshold) imageDataVector.erase(imageDataVector.end() - j); } This code works fine for almost all cases, however if all elements of vector meets the criteria I get an error: vector erase iterator outside the range This error occurs if I have only one element

Why can't I delete last element of vector

和自甴很熟 提交于 2021-02-07 14:24:27
问题 I have stl vector consisting of several elements. I need to iterate through this vector and delete elements which meets some criteria. So I wrote this code for (int j = imageDataVector.size()-1; j >= 0; j--) { if(imageDataVector[i] < threshold) imageDataVector.erase(imageDataVector.end() - j); } This code works fine for almost all cases, however if all elements of vector meets the criteria I get an error: vector erase iterator outside the range This error occurs if I have only one element