boost::python: Python list to std::vector
To make your C++ method accept Python lists you should use boost::python::list void massadd(boost::python::list& ns) { for (int i = 0; i < len(ns); ++i) { add(boost::python::extract<double>(ns[i])); } }
To make your C++ method accept Python lists you should use boost::python::list void massadd(boost::python::list& ns) { for (int i = 0; i < len(ns); ++i) { add(boost::python::extract<double>(ns[i])); } }
This is LWG2116. The choice between moving and copying the elements is often expressed as std::is_nothrow_move_constructible, i.e. noexcept(T(T&&)), which also erroneously checks the destructor.
Since your inner dimension is constant, I think you want std::vector< std::array<int, 5> > vecs; vecs.reserve(N); This will give you preallocated contiguous storage, which is optimal for performance.
Use iterators. std::vector<int> lines; // fill std::size_t const half_size = lines.size() / 2; std::vector<int> split_lo(lines.begin(), lines.begin() + half_size); std::vector<int> split_hi(lines.begin() + half_size, lines.end()); Since iterator ranges represent half open ranges [begin, end), you don’t need to add 1 to the second begin iterator: lines.begin() + half_size isn’t copied to the first vector. Note that things … Read more
Vectors – No. Because the capacity of vectors never shrinks, it is guaranteed that references, pointers, and iterators remain valid even when elements are deleted or changed, provided they refer to a position before the manipulated elements. However, insertions may invalidate references, pointers, and iterators. Lists – Yes, inserting and deleting elements does not invalidate … Read more
I’d say the exceptions that vector::at() throws aren’t really intended to be caught by the immediately surrounding code. They are mainly useful for catching bugs in your code. If you need to bounds-check at runtime because e.g. the index comes from user input, you’re indeed best off with an if statement. So in summary, design … Read more
It doesn’t make a difference as far as construction of the new object is concerned; you already have a unique_ptr<Foo> prvalue (the result of the call to make_unique) so both push_back and emplace_back will call the unique_ptr move constructor when constructing the element to be appended to the vector. If your use case involves accessing … Read more
You can use std::transform as: std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert); Which requires you to implement convert() as: char *convert(const std::string & s) { char *pc = new char[s.size()+1]; std::strcpy(pc, s.c_str()); return pc; } Test code: int main() { std::vector<std::string> vs; vs.push_back(“std::string”); vs.push_back(“std::vector<std::string>”); vs.push_back(“char*”); vs.push_back(“std::vector<char*>”); std::vector<char*> vc; std::transform(vs.begin(), vs.end(), std::back_inserter(vc), convert); for ( size_t i = 0 … Read more
Just do: MyClass::MyClass(int m_size) : size(m_size), vec(m_size, 0) You already seem to know about initializer lists, why not initialize vector there directly? vec = new vector<int>(size,0); is illegal because new returns a pointer and in your case vec is an object. Your second option: vector<int> temp(size,0); vec = temp; although it compiles, does extra work … Read more
If you define your Point as having contiguous data storage (e.g. struct Point { int a; int b; int c; } or using std::array), then std::vector<Point> will store the Points in contiguous memory locations, so your memory layout will be: p0.a, p0.b, p0.c, p1.a, p1.b, p1.c, …, p(N-1).a, p(N-1).b, p(N-1).c On the other hand, if … Read more