stdvector

Do vector.emplace_back() and vector.push_back() do the same thing?

非 Y 不嫁゛ 提交于 2020-11-30 06:42:21
问题 So I was trying to add integers onto the back of my vector and mistakenly thought push_back() added the new data onto the front of the vector (aka vector[0]). I did a test in Xcode and tested push_back() against emplace_back() and got the same results. I thought they were different, but this makes me think that maybe they do the same thing. If this is so, why does vector have the different methods? Here's my code in case I was doing: #include <vector> #include <iostream> using namespace std ;

When std::vector reallocate its memory array, is copy constructor or move constructor used?

二次信任 提交于 2020-11-29 03:31:59
问题 When std::vector reallocate its memory array, what kind of copy / move constructor is used to copy / move elements to new houses? 回答1: If the move-constructor exists and is noexcept then it is used. Otherwise the copy-constructor is used. Using a move-constructor that might throw is undesirable as it might happen that some objects are moved to the new storage and then an exception prevents the rest of the objects being moved. The cppreference.com site does say that if the object is non

When std::vector reallocate its memory array, is copy constructor or move constructor used?

混江龙づ霸主 提交于 2020-11-29 03:28:41
问题 When std::vector reallocate its memory array, what kind of copy / move constructor is used to copy / move elements to new houses? 回答1: If the move-constructor exists and is noexcept then it is used. Otherwise the copy-constructor is used. Using a move-constructor that might throw is undesirable as it might happen that some objects are moved to the new storage and then an exception prevents the rest of the objects being moved. The cppreference.com site does say that if the object is non

When std::vector reallocate its memory array, is copy constructor or move constructor used?

流过昼夜 提交于 2020-11-29 03:28:04
问题 When std::vector reallocate its memory array, what kind of copy / move constructor is used to copy / move elements to new houses? 回答1: If the move-constructor exists and is noexcept then it is used. Otherwise the copy-constructor is used. Using a move-constructor that might throw is undesirable as it might happen that some objects are moved to the new storage and then an exception prevents the rest of the objects being moved. The cppreference.com site does say that if the object is non

2d char vector printing spaces when printing second column

故事扮演 提交于 2020-07-22 06:00:10
问题 Here I have a 2d vector of char - std::vector<std::vector<char>> solution = { {"O","1"}, {"T","0"}, {"W","9"}, {"E","5"}, {"N","4"} }; Printing anything from first column - prints fine. cout << "In main - " << solution [ 1 ] [ 0 ]; // Prints T But when I try to access element of second column. cout << "In main - " << solution [ 1 ] [ 1 ]; // Prints blank space - I can't seem to understand why's the case. After quiet amount of searching I tried by putting single quotes around every element.

Why isn't a vector of a vector guaranteed to be contiguous? (or is it?)

寵の児 提交于 2020-07-09 16:46:53
问题 I know std::vector elements are guaranteed contiguous in memory. So then why can't you expect a vector containing other vectors to have the total collection contiguous? The vector is supposed to guarantee contiguous memory layout for its enclosed items, and if those enclosures are also vectors, then I'd expect the full contents of the top-most vector to be in contiguous memory. But there seems to be some contention on this issue as to whether or not this is true. Can one safely rely on it or

Why isn't a vector of a vector guaranteed to be contiguous? (or is it?)

大兔子大兔子 提交于 2020-07-09 16:45:06
问题 I know std::vector elements are guaranteed contiguous in memory. So then why can't you expect a vector containing other vectors to have the total collection contiguous? The vector is supposed to guarantee contiguous memory layout for its enclosed items, and if those enclosures are also vectors, then I'd expect the full contents of the top-most vector to be in contiguous memory. But there seems to be some contention on this issue as to whether or not this is true. Can one safely rely on it or

how to convert std::vector<vector> to void*

核能气质少年 提交于 2020-06-25 01:21:11
问题 I am wondering how can I convert std::vector to void*, for example, std::vector<ColorData> piexls_ (w*h, background_color); Now I want to convert piexls_ to (void*) so that I can memcpy the pixels_. memcpy ((void*)destinationBuffer->pixels_, (void*)sourceBuffer->pixels_, \ sizeof(ColorData)*destinationBuffer->width_*destinationBuffer->height_); But when I run this code, I got the error says that invalid cast from type ‘std::vector<image_tools::ColorData>’ to type ‘void*’ How can I convert std