stdvector

boost::python: Python list to std::vector

末鹿安然 提交于 2019-11-29 21:26:00
Finally I'm able to use std::vector in python using the [] operator. The trick is to simple provide a container in the boost C++ wrapper which handles the internal vector stuff: #include <boost/python.hpp> #include <vector> class world { std::vector<double> myvec; void add(double n) { this->myvec.push_back(n); } std::vector<double> show() { return this->myvec; } }; BOOST_PYTHON_MODULE(hello) { class_<std::vector<double> >("double_vector") .def(vector_indexing_suite<std::vector<double> >()) ; class_<World>("World") .def("show", &World::show) .def("add", &World::add) ; } The other challenge is:

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

笑着哭i 提交于 2019-11-29 20:13:50
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] = !floodedCellsTimeInterval[i]; qInflow[i] = qInflow[i] + 1; } It iterates through different arrays with a size of

keep std vector/list sorted while insert, or sort all

三世轮回 提交于 2019-11-29 19:52:36
问题 Lets say I have 30000 objects in my vector/list. Which I add one by one. I need them sorted. Is it faster to sort all at once (like std::sort), or keep vector/list sorted while I add object one by one? vector/list WILL NOT be modified later. 回答1: When you are keeping your vector list sorted while inserting elements one by one , you are basically performing an insertion sort, that theoretically runs O(n^2) in worst case. The average case is also quadratic, which makes insertion sort

type requirements for std::vector<type>

坚强是说给别人听的谎言 提交于 2019-11-29 15:47:57
I am still confused about the requirements for a type to be used with a std::vector in C++11, but this may be caused by a buggy compiler (gcc 4.7.0). This code: struct A { A() : X(0) { std::cerr<<" A::A(); this="<<this<<'\n'; } int X; }; int main() { std::vector<A> a; a.resize(4); } works fine and produces the expected output, indicating that the default ctor (explicitly given) is called (and not an implicit copy ctor). However, if I add a deleted copy ctor to the class, viz struct A { A() : X(0) { std::cerr<<" A::A(); this="<<this<<'\n'; } A(A const&) = delete; int X; }; gcc 4.7.0 does not

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

旧街凉风 提交于 2019-11-29 15:24:37
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 <string> using namespace std; #include <sys/types.h> #include <sys/wait.h> #include <signal.h> Any

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

这一生的挚爱 提交于 2019-11-29 14:12:11
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? heretolearn You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and

Using std::sort() without prefix “std” and also without “using namespace std;” compiles successfully

心已入冬 提交于 2019-11-29 14:10:34
问题 As sort() is defined in namespace std it must always be used as std::sort .But the following code compiles correctly even without std . #include <vector> #include <algorithm> int main() { std::vector<int> nums = {4,3,1,7,2,0}; sort(nums.begin(),nums.end()); } ideone.com But this code doesn't. #include <array> #include <algorithm> int main() { std::array<int,5> nums = {4,1,8,9,6}; sort(nums.begin(),nums.end()); } Using gcc 4.8.4 with -std=c++11 flag enabled. From both these code snippets it is

Vector constructor with two parameters is parsed as a function declaration

99封情书 提交于 2019-11-29 11:02:34
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() { std::string sen = "abc def ghi jkl"; std::istringstream iss(sen); std::vector<std::string> // declaration in question vec(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>()); std::copy(vec.begin(), vec.end(), std::ostream_iterator<std::string>(std::cout, "\n")); } The compiler throws an error at the call to std::copy request for member 'begin' in 'vec', which is of non-class type... I can get around the error like this: std::istream_iterator<std::string> it

vector vs map performance confusion

让人想犯罪 __ 提交于 2019-11-29 10:55:33
问题 edit: I am specifically comparing std::vector 's linear search operations to the std::map binary search operations because that is what Herb's claim seemed to relate to. I know that using a binary search will move the performance from O(N) to O(log N) but that would not be testing Herb's claim Bjarne Stroustrup and Herb Sutter have both recently talked about how awesome std::vector is in situations one would expect std::list to be used, owing to the cost of cache misses during linked list

Set std::vector<int> to a range

不想你离开。 提交于 2019-11-29 06:06:24
问题 What's the best way for setting an std::vector<int> to a range, e.g. all numbers between 3 and 16? 回答1: You could use std::iota if you have C++11 support or are using the STL: std::vector<int> v(14); std::iota(v.begin(), v.end(), 3); or implement your own if not. If you can use boost , then a nice option is boost::irange: std::vector<int> v; boost::push_back(v, boost::irange(3, 17)); 回答2: std::vector<int> myVec; for( int i = 3; i <= 16; i++ ) myVec.push_back( i ); 回答3: See e.g. this question