stdvector

Reading and writing a std::vector into a file correctly

拟墨画扇 提交于 2019-11-27 07:27:15
That is the point. How to write and read binary files with std::vector inside them? I was thinking something like: //============ WRITING A VECTOR INTO A FILE ================ const int DIM = 6; int array[DIM] = {1,2,3,4,5,6}; std::vector<int> myVector(array, array + DIM); ofstream FILE(Path, ios::out | ofstream::binary); FILE.write(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6); //=========================================================== But I don't know how to read this vector. Because I thought that the following was correctly but it isn't: ifstream FILE(Path, ios::in |

vector::at vs. vector::operator[]

我是研究僧i 提交于 2019-11-27 06:11:09
I know that at() is slower than [] because of its boundary checking, which is also discussed in similar questions like C++ Vector at/[] operator speed or ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster! . I just don't understand what the at() method is good for. If I have a simple vector like this one: std::vector<int> v(10); and I decide to access its elements by using at() instead of [] in situation when I have a index i and I'm not sure if its in vectors bounds, it forces me to wrap it with try-catch block : try { v.at(i) = 2; } catch (std::out_of_range

std::dynarray vs std::vector

╄→гoц情女王★ 提交于 2019-11-27 05:23:33
问题 C++14 presents std::dynarray: std::dynarray is a sequence container that encapsulates arrays with a size that is fixed at construction and does not change throughout the lifetime of the object. std::dynarray must be allocated in run-time as same as std::vector . So what are the benefits and the usage of std::dynarray while we can use std::vector which is more dynamic (and also re-sizable)? 回答1: So what are the benefits and the usage of std::dynarray , when we can use std::vector which is more

What is better: reserve vector capacity, preallocate to size or push back in loop?

巧了我就是萌 提交于 2019-11-27 02:22:50
问题 I have a function that takes a pointer to char array and segment size as input arguments and calls another function that requires a std::array<std::string> . The idea is that the input char array is "sectioned" into equal parts, and string array formed. The input char array format is several smaller arrays (or strings) of determined size, concatenated togeather. These are not assumed zero-terminated, although they might be. Examples for segment size 5 and number of elements 10: char k[] =

Vector of const objects giving compile error

自作多情 提交于 2019-11-27 02:03:32
I have declared the following in my code vector <const A> mylist; I get the following compile error - new_allocator.h:75: error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const \[with _Tp = const A]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const A]' cannot be overloaded But if declare - vector <A> mylist; my code compiles. Is const not allowed in this context? I am copying my code here for everyone'e reference - #include <iostream> #include <vector> using namespace std; class A { public: A () {cout << "default constructor\n";} A (int i): m

Why doesn't vector::clear remove elements from a vector?

爱⌒轻易说出口 提交于 2019-11-27 01:48:48
When I use clear() on a std::vector , it is supposed to destroy all the elements in the vector , but instead it doesn't. Sample code: vector<double> temp1(4); cout << temp1.size() << std::endl; temp1.clear(); cout << temp1.size() << std::endl; temp1[2] = 343.5; // I should get segmentation fault here .... cout << "Printing..... " << temp1[2] << endl; cout << temp1.size() << std::endl; Now, I should have gotten segmentation fault while trying to access the cleared vector, but instead it fills in the value there (which according to me is very buggy) Result looks as follows: 4 0 Printing..... 343

Performance issue for vector::size() in a loop in C++

孤者浪人 提交于 2019-11-27 01:17:37
In the following code: std::vector<int> var; for (int i = 0; i < var.size(); i++); Is the size() member function called for each loop iteration, or only once? In theory , it is called each time, since a for loop: for(initialization; condition; increment) body; is expanded to something like { initialization; while(condition) { body; increment; } } (notice the curly braces, because initialization is already in an inner scope) In practice , if the compiler understands that a piece of your condition is invariant through all the duration of the loop and it does not have side-effects , it can be

Easiest way to make a cyclic iterator (circulator)?

余生颓废 提交于 2019-11-27 01:04:43
I have an object that I want to travel in a continuous loop in a game. I have a series of coordinates in a std::vector that I want to use as waypoints. Is there any way to make an std::vector<T>::iterator cyclic (also known as a circulator)? The best I can come up with is to have two iterators and then whenever the first iterator is exhausted assign to it the value of the second (which would not be used to do anything else) but I am not even sure it will work - will the assignment operator copy whatever the iterator is using to hold the index or will it merely be referenced (and therefore will

How to push_back without operator=() for const members?

只谈情不闲聊 提交于 2019-11-26 22:07:43
问题 How to push_back() to a C++ std::vector without using operator=() for which the default definition violates having const members? struct Item { Item(int value) : _value(value) { } const char _value; } vector<Item> items; items.push_back(Item(3)); I'd like to keep the _value const since it should not change after the object is constructed, so the question is how do I initialize my vector with elements without invoking operator=()? Here is the basic error the g++ v3.4.6 is giving me: .../3.4.6

std::vector of std::vectors contiguity

左心房为你撑大大i 提交于 2019-11-26 21:48:38
问题 I know that std::vector<T> internally stores it's data contiguously (unless it is std::vector<bool> ) both in the old C++03 standard and the new C++11 . Nice stackoverflow questions that deal with this and quote the standard: answer, answer. What about the data inside nested vectors std::vector <std::vector <T> > ? How is that stored? If every internal vector needs to store it's data contiguously, how can it be true that &v[n] == &v[0] + n for all 0 <= n < v.size() . To phrase this slightly