stdvector

Is begin() == end() for any empty() vector?

梦想与她 提交于 2019-12-03 14:17:35
问题 I have long assumed that for any empty std::vector V , V.begin() == V.end() . Yet I see nothing in the C++ specification that states this to always be true. Is it necessarily true or does it just happen to be true on most implementations? 回答1: Yes, that's what the standard requires it to be for empty() for any container. § 23.2.1 Table 96 of the C++11 standard says: +----------+---------------+----------------------+ |Expression| Return Type | Operational Semantics| |----------|--------------

Getting the size in bytes of a vector [duplicate]

心不动则不痛 提交于 2019-12-03 12:49:30
This question already has an answer here: sizeof() std::vector (C++) 2 answers Sorry for this maybe simple and stupid question but I couldn't find it anywhere. I just don't know how to get the size in bytes of a std::vector. std::vector<int>MyVector; /* This will print 24 on my system*/ std::cout << "Size of my vector:\t" << sizeof(MyVector) << std::endl; for(int i = 0; i < 1000; i++) MyVector.push_back(i); /* This will still print 24...*/ std::cout << "Size of my vector:\t" << sizeof(MyVector) << std::endl; So how do I get the size of a vector?! Maybe by multiplying 24 (vector size) by the

How to define a 2D array in C++ and STL without memory manipulation?

不羁岁月 提交于 2019-12-03 12:42:44
问题 There are several ways to define a 2D array in C++ and STL without memory manipulation, and the following codes illustrate two different methods: int main () { /************** 1 2 3 4 5 6 ***************/ // Method 1 const int ROW = 2; const int COL = 3; int array1[ROW][COL]; for(int i=0; i<ROW; i++) for(int j=0; j<COL; j++) array1[i][j] = i*COL+j+1; // Method 2 typedef vector<vector<int> > ARRAY; ARRAY array2; vector<int> rowvector; for(int i=0; i<ROW; i++) { rowvector.clear(); for(int j=0;

Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

戏子无情 提交于 2019-12-03 10:53:24
问题 What is the proper way of transferring ownership of a std::vector<unique_ptr<int> > to a class being constructed? Below is a code representation of what I want to do. I realize it is not correct (won't compile) and violates "uniqueness" whether I pass the vector to the constructor by value or by reference. I want Foo to be the new owner of the vector, and want the calling function to relinquish ownership. Do I need the constructor to take a std::unique_ptr<std::vector<std::unique_ptr<int> > >

How do I convert an armadillo matrix to a vector of vectors?

偶尔善良 提交于 2019-12-03 05:44:00
I created an armadillo c++ matrix as follows: arma::mat A; A.zeros(3,4); I want to convert it to a vector of vectors defined by std::vector< std::vector<double> > B(3, std::vector<double>(4) ); How do I set B to equal A? If there is not an easy way for a vector of vectors, what about an array of arrays, i.e., what if I defined B to be double B[3][4]; In such cases you should use arma::conv_to which is a totally superb feature of arma. Note that this method will require from a source object to be able to be interpreted as a vector. That is why we need to do this iteratively for every row. Here

Returning an STL vector from a function - copy cost

只谈情不闲聊 提交于 2019-12-03 05:04:30
问题 When you return an stl vector from a function: vector<int> getLargeArray() { ... } Is the return going to be an expensive copy operation? I remember reading somewhere that vector assignment being fast -- should I require the caller to pass a reference instead? void getLargeArray( vector<int>& vec ) { ... } 回答1: Assuming your function constructs and returns new data, you should return by value, and try to make sure that the function itself has one return point that returns a variable of type

Is it good practice to use std::vector as a simple buffer?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 04:54:22
问题 I have an application that is performing some processing on some images. Given that I know the width/height/format etc. (I do), and thinking just about defining a buffer to store the pixel data: Then, rather than using new and delete [] on an unsigned char* and keeping a separate note of the buffer size, I'm thinking of simplifying things by using a std::vector . So I would declare my class something like this: #include <vector> class MyClass { // ... etc. ... public: virtual void

advantages of std::set vs vectors or maps

故事扮演 提交于 2019-12-03 04:32:50
问题 This may be a stupid question, I am quite new to C++ and programming in general. I wish to understand the use of several STL containers and with that in mind, I was wondering what the advantages are of using std::set vs for example using vectors or maps? I can't seem to find an explicit answer to this question. I noticed that sets use maps, but then why not always use maps or always use sets. Instead 2 quite similar containers are provided. Thanks in advance. 回答1: Both std::set and std::map

C++ One std::vector containing template class of multiple types

匆匆过客 提交于 2019-12-03 04:32:47
问题 I need to store multiple types of a template class in a single vector. Eg, for: template <typename T> class templateClass{ bool someFunction(); }; I need one vector that will store all of: templateClass<int> t1; templateClass<char> t2; templateClass<std::string> t3; etc As far as I know this is not possible, if it is could someone say how? If it isn't possible could someone explain how to make the following work? As a work around I tried to use a base, non template class and inherit the

Is begin() == end() for any empty() vector?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 04:08:43
I have long assumed that for any empty std::vector V , V.begin() == V.end() . Yet I see nothing in the C++ specification that states this to always be true. Is it necessarily true or does it just happen to be true on most implementations? Yes, that's what the standard requires it to be for empty() for any container. § 23.2.1 Table 96 of the C++11 standard says: +----------+---------------+----------------------+ |Expression| Return Type | Operational Semantics| |----------|---------------|----------------------| |a.empty() |Convertible |a.begin() == a.end() | | |to bool | | | | | | +----------