stdvector

Create a fixed size std::vector and write to the elements

北城余情 提交于 2019-12-10 01:19:21
问题 In C++ I wish to allocate a fixed-size (but size determined at runtime) std::vector then write to the elements in this vector. This is the code I am using: int b = 30; const std::vector<int> test(b); int &a = test[3]; However, this gives me a compiler (MSVC 2010 Pro) error: error C2440: 'initializing' : cannot convert from 'const int' to 'int &'. Conversion loses qualifiers. My understanding of const is that it makes all of the member variables of a class constant. For example, the following

Why does std::vector work with incomplete types in class definitions?

社会主义新天地 提交于 2019-12-09 16:41:07
问题 The following question came up: The c++ standard seems to say, that std::vector requires a complete type to work. (See https://en.cppreference.com/w/cpp/container/vector ) Then, why does the following code still compile? #include <vector> struct parent; struct child { std::vector<parent> parents; //parent is incomplete here! }; struct parent { std::vector<child> children; }; This seems counterintuitive. If std::vector requires a complete type, then std::vector<parent> should not compile

Initializing a 2D vector using initialization list in C++11

拥有回忆 提交于 2019-12-09 16:13:40
问题 How can i initialize a 2D vector using an initialization list? for a normal vector doing : vector<int> myvect {1,2,3,4}; would suffice. But for a 2D one doing : vector<vector<int>> myvect{ {10,20,30,40}, {50,60,70,80} }; What is a correct way of doing it? And how can i iterate through it using for? for(auto x: myvect) { cout<<x[j++]<<endl; } this for only shows: 10,1 ! And by the way what does this mean ? vector<int> myvect[5] {1,2,3,4}; i saw it here and cant understand it! Link 回答1: What is

C++: Automatic vector reallocation invokes copy constructors? Why?

心不动则不痛 提交于 2019-12-09 09:53:49
问题 I'm reading C++ Primer, 3rd Ed (Lippman and Lajoie) and it's saying that when a vector needs to be reallocated in order to make space for more elements added with push_back() , the elements are copy-constructed in the new space and then the destructor is called on the old elements. I'm confused about why this is necessary - why can't the data just be copied bit-for-bit? I assume that the answer has to do with dynamic memory allocation, but my current line of reasoning is that even if the

“static const int” causes linking error (undefined-reference)

怎甘沉沦 提交于 2019-12-09 02:39:43
问题 I am baffled by the linker error when using the following code: // static_const.cpp -- complete code #include <vector> struct Elem { static const int value = 0; }; int main(int argc, char *argv[]) { std::vector<Elem> v(1); std::vector<Elem>::iterator it; it = v.begin(); return it->value; } However, this fails when linking -- somehow it needs to have a symbol for the static const "value." $ g++ static_const.cpp /tmp/ccZTyfe7.o: In function `main': static_const.cpp:(.text+0x8e): undefined

Behavior of vector's reserve( ) method

◇◆丶佛笑我妖孽 提交于 2019-12-08 21:26:14
问题 I wanted to know the behavior of std::vector::reserve() in following situations: Suppose reserve(N) is called multiple times one after another immediately. Will the earlier reserve(N1) get added up or overwritten ? If the earlier reserve(N1) gets overwritten with the latest call, then what happens if the latest reserve(Nn) demands less number of slots ? After declaring vector if we have simply push_back() X elements, and then we call reserve(N) . Will the already push_back() X elements

OpenCV cv::Mat causing potential memory leak with std::vector

邮差的信 提交于 2019-12-08 19:53:36
As it stands right now im trying to save an entire list of images in the form of cv::Mats inside of a vector for later processing. Right now I have something that looks like this: do { image = readimage(); cv::Mat mat = cv::Mat((length, width, CV_8UC4, image)); cv::Mat temp = mat.clone(); saved_images.push_back(); mat.release(); temp.release(); freeimagememory(image); } while(hasimage); This actually works. For exceptionally small lists of images it will store them just fine. However as I get to large amounts of images the program consistently crashes saying Abort() was called, and upon

Should my function return a pointer to std::vector, or a reference to std::vector?

大城市里の小女人 提交于 2019-12-08 18:28:45
问题 I have a std::map<int, std::vector<SomeStruct>> , and provide a query like std::vector<SomeStruct> FindData(int key) . To prevent copying the whole data, I modify it to be std::vector<SomeStruct>& FindData(int key) . But, there will be no data for certain key , so sometimes I have nothing to return. In that case, I declare a file scope variable that is an empty std::vector<SomeStruct> and return it. But if I choose the pointer to vector, that is std::vector<SomeStruct>* FindData(int key) then

Implement support for std::vector without std_vector.i

别等时光非礼了梦想. 提交于 2019-12-08 08:23:44
Okay, I've already asked 2 questions about my problem and despite the fact that the replies were really helpful, I am not able to find an optimal solution for my problem. Let me explain my main objective/problem now. Due to some constraints I can't use std_vector.i in my swig interface, but I need to use a C++ object of (vector of vectors of string) vector<vector<string>> in Python. I implemented a solution where I am converting whole vector<vector<string> > to Python "List of Lists" wherein I am doing the following conversions: each C++ string to Python String using PyString_FromString() each

convert function return from ‘std::vector<QString>’ to ‘QVariant’

纵饮孤独 提交于 2019-12-08 07:04:29
问题 I am working on a QT based application.One of of my class is a child class of QAbstractTableModel. The data function has a return type of QVariant(Union).But i want to return a custom type std::vector<QString> Came to know about Q_DECLARE_METATYPE(); It makes new types available to QVariant. -Test Case Code- #include <QApplication> #include <QMetaType> #include <vector> #include<QVariant> Q_DECLARE_METATYPE(std::vector<QString>); QVariant data(int role) { std::vector<QString> test1; test1