stdvector

C++ standard vector resize() function

倾然丶 夕夏残阳落幕 提交于 2019-12-07 08:42:54
问题 resize() adds/removes elements based on the size given to it. reserve() reserves memory space and it will not reallocate memory. The question I have is whether resize also works the same as in the capacity of the vector will only not increase? To add, would a combination of: std::vector<X> vector; vector.reserve(5); vector.resize(5); make any sense? Is it redundant? The goal here is to be able to overwrite values in the vector without having the vector allocate any extra space. 回答1: From this

overloading *, +, -'operators for vector<double> class

烈酒焚心 提交于 2019-12-07 07:13:16
问题 I'm writing a Line class to make numerical methods and I want these operators (*, +, -) to make my code more readable and easier to understand. #include <vector> using namespace std; typedef vector<double> Vector; class Line : public Vector { public: Line(); ~Line(); Line operator+(Line); Line operator-(Line); Line operator*(double); }; Line Line::operator*(double alfa) { Line temp; int n = size(); temp.resize(n); for (int i = 0; i < n; i++) { temp.at(i) = this->at(i)*alfa; } return temp; }

How to store a vector of objects of an abstract class which are given by std::unique_ptr?

China☆狼群 提交于 2019-12-07 06:05:29
问题 I got a loop in which i use a function returning std::unique_ptr to an object of an abstract class. I want to store these objects into a std::vector via push_back. But since the objects are of abstract type i get the following error: error: cannot allocate an object of abstract type for the line cells.push_back(std::move(*cell)); where cells is a std::vector of the abstract type and cell is of type std::unique_ptr<AbstractType>&& cell (I actually pass cell to a handler class) I know that one

Combine multiple vectors (results of function) into one with template

岁酱吖の 提交于 2019-12-07 05:20:58
问题 I'd like to have a templated function taking in a vector<T> v and a function op, mapping T to vector<U> and would like to concatenate the results of applying f to every element vector of v to return a vector<U> = [ Elements of op(v[0]), Elements of op(v[1]) ...]. A working option I found was adding an example in the function to allow for template deduction: template <typename Container> Container& concat(Container& c1, Container const& c2) { c1.insert(end(c1), begin(c2), end(c2)); return c1;

Best way to delete a std::unique_ptr from a vector with a raw pointer?

一个人想着一个人 提交于 2019-12-07 03:46:33
问题 So I have a vector like so: std::vector<std::unique_ptr<SomeClass>> myVector; Then I have another vector which contains raw pointers of SomeClass : std::vector<SomeClass*> myOtherVector; If there is an element inside myOtherVector it will also be inside myVector , so I want to go through each element in myOtherVector and remove the same element from myVector . Then clear out the vector. This is what I came up with: for(size_t i = 0; i < myOtherVector.size(); i++) { myVector.erase(std::remove

Destroy std::vector without releasing memory

自闭症网瘾萝莉.ら 提交于 2019-12-06 20:22:36
问题 Lets say I have a function to get data into an std vector: void getData(std::vector<int> &toBeFilled) { // Push data into "toBeFilled" } Now I want to send this data to another function, that should free the data when finished: void useData(int* data) { // Do something with the data... delete[] data; } Both functions (getData and useData) are fixed and cannot be changed. This works fine when copying the data once: { std::vector<int> data; getData(data); int *heapData = new int[data.size()];

Is inserting an element of a std::vector into the same vector allowed?

心不动则不痛 提交于 2019-12-06 17:14:00
问题 Consider the following insert and emplace member functions of std::vector<T> : template <class... Args> iterator emplace(const_iterator position, Args&&... args); iterator insert(const_iterator position, const T& x); iterator insert(const_iterator position, T&& x); iterator insert(const_iterator position, size_type n, const T& x); What if one of them is invoked with a reference to an element of the vector itself as an argument? Normally, each of them invalidates references to all elements

How to automatically maintain a list of class instances?

感情迁移 提交于 2019-12-06 14:21:39
In my C++ project, I have an Engine class and an Object class. My issue lies with how my instances of Object are created. Currently this is done through the use of a CreateObject(parameters) function in the Engine class. This adds a new instance of Object to an std::vector of Object instances. I want to maintain this list of instances of Object in my Engine class, but without the need for the CreateObject(parameters) function. My reason for this is so that I can create new classes that can inherit from Object but still be added to this list. The reason for this list is so that (in Engine ) I

deleting element objects of a std vector using erase : a) memory handling and b) better way?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 11:46:06
I have a vec_A that stores instances of class A as: vec_A.push_back(A()); I want to remove some elements in the vector at a later stage and have two questions: a) The element is deleted as: vec_A.erase(iterator) Is there any additional code I need to add to make sure that there is no memory leak? . b) Assume that condition if(num <5) is if num is among a specific numberList. Given this, is there a better way to delete the elements of a vector than what I am illustrating below? #include<vector> #include<stdio.h> #include<iostream> class A { public: int getNumber(); A(int val); ~A(){}; private:

How does a C++ std::container (vector) store its internals (element address, access by index)?

混江龙づ霸主 提交于 2019-12-06 07:37:23
I am trying to "hack" a game (Red Alert 3), I try to make a program which shows the unit list of my opponents. As for that I first need to find a (static) pointer to my own list which I can do on single player. I have noticed this behaviour: (by looking at which addresses are changed by the add_unit code): if a units hasn't been build yet, create a new address for it (random?) and set the value to 1 (amount of units of that type) when the unit has been already build once in the game, increment the original address of the unit type by 1 This looks to me like std::vector behaviour. Now I am