stdvector

std::vector for parent and child class

旧街凉风 提交于 2021-02-18 18:51:58
问题 I need to create a vector that can contain my parent class and sub class data. This is what i do.. Vehicle is the parent class Car is the child class About Car.cpp , it got the following struct Point { int x,y }; class Car : public Vehicle { private: Point wheelPoint[4]; double area; public: void setPoint(); }; void Car::setPoint() { int xData,yData; cout << "Please enter X:"; cin >> xData; cout << "Please enter Y:"; cin >> yData; wheelPoint[i].x = xData; wheelPoint[i].y = yData; } Then at my

How to remove all instances of a duplicate from a vector<int> [duplicate]

余生颓废 提交于 2021-02-16 17:57:12
问题 This question already has answers here : How to remove duplicated items in a sorted vector (7 answers) Closed yesterday . I've been trying to solve an "easy" leetcode question for two hours now where I need to remove every instance of an int that appears more than once then add the non dupes together. I've tried about 10 different ways and I can only get it to go down to one copy of each int. Here is the nicest solution I've written, but given input {1,2,2,3,4} it would return {1,2,3,4}

Iterating a vector in reverse order with a range-for loop? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 06:58:41
问题 This question already has answers here : C++11 reverse range-based for-loop (9 answers) Closed 10 months ago . I want to iterate a vector in reverse order , i know that i can do it easily by below code #include<bits/stdc++.h> using namespace std; int main() { vector<int> v(10); for(auto it=v.rbegin();it!=v.rend();it++) cout<<*it<<""; } For iterating in forward we can simply do by below code. for(auto it:v) cout<<it<<" "; Now task is ... Is it possible to do reverse iterate similar to for(auto

Iterating a vector in reverse order with a range-for loop? [duplicate]

只愿长相守 提交于 2021-02-10 06:58:02
问题 This question already has answers here : C++11 reverse range-based for-loop (9 answers) Closed 10 months ago . I want to iterate a vector in reverse order , i know that i can do it easily by below code #include<bits/stdc++.h> using namespace std; int main() { vector<int> v(10); for(auto it=v.rbegin();it!=v.rend();it++) cout<<*it<<""; } For iterating in forward we can simply do by below code. for(auto it:v) cout<<it<<" "; Now task is ... Is it possible to do reverse iterate similar to for(auto

C++ - Safety of accessing element of vector via pointers

你离开我真会死。 提交于 2021-02-07 20:40:28
问题 In a C++ project of mine, I am using a vector to hold a bunch of struct s which hold a number of elements for a simple game (ie: tic-tac-toe, coordinates, x vs o , etc). ie: struct gameMove { int x; int y; int player; int order; }; Every time during a single game, whenever a player makes a move (ie: places an x or o ), the information is stored in the vector via push_back() , to create an "undo" feature, which currently works as expected. In some parts of my undo/redo logic, I am using

Why doesn't C++ require a “new” statement to initialize std::vector?

筅森魡賤 提交于 2021-02-05 20:10:22
问题 /* bar.h */ class bar{ /* standard stuff omitted */ std::vector<my_obj*> foo; }; /* bar.cpp */ bar::bar(){ // foo = new std::vector<my_obj*>(); <-- why don't I need this line?? foo.push_back(new my_obj()); } Why does this code work even though we didn't assign foo a new instance of std::vector ? 回答1: Because C++ is not C#/Java. std::vector<my_obj*> foo; This is a definition of an object , not a reference as in C#/Java. An object is a living instance of a type. new std::vector<my_obj*>() This

Why doesn't C++ require a “new” statement to initialize std::vector?

倾然丶 夕夏残阳落幕 提交于 2021-02-05 20:05:50
问题 /* bar.h */ class bar{ /* standard stuff omitted */ std::vector<my_obj*> foo; }; /* bar.cpp */ bar::bar(){ // foo = new std::vector<my_obj*>(); <-- why don't I need this line?? foo.push_back(new my_obj()); } Why does this code work even though we didn't assign foo a new instance of std::vector ? 回答1: Because C++ is not C#/Java. std::vector<my_obj*> foo; This is a definition of an object , not a reference as in C#/Java. An object is a living instance of a type. new std::vector<my_obj*>() This

Why doesn't C++ require a “new” statement to initialize std::vector?

烈酒焚心 提交于 2021-02-05 19:55:44
问题 /* bar.h */ class bar{ /* standard stuff omitted */ std::vector<my_obj*> foo; }; /* bar.cpp */ bar::bar(){ // foo = new std::vector<my_obj*>(); <-- why don't I need this line?? foo.push_back(new my_obj()); } Why does this code work even though we didn't assign foo a new instance of std::vector ? 回答1: Because C++ is not C#/Java. std::vector<my_obj*> foo; This is a definition of an object , not a reference as in C#/Java. An object is a living instance of a type. new std::vector<my_obj*>() This

Using std::vector as view on to raw memory

自古美人都是妖i 提交于 2021-02-05 14:23:07
问题 I'm using a external library which at some point gives me a raw pointer to an array of integers and a size. Now I'd like to use std::vector to access and modify these values in place, rather than accessing them with raw pointers. Here is an articifial example that explains the point: size_t size = 0; int * data = get_data_from_library(size); // raw data from library {5,3,2,1,4}, size gets filled in std::vector<int> v = ????; // pseudo vector to be used to access the raw data std::sort(v.begin

Using std::vector as view on to raw memory

混江龙づ霸主 提交于 2021-02-05 14:20:47
问题 I'm using a external library which at some point gives me a raw pointer to an array of integers and a size. Now I'd like to use std::vector to access and modify these values in place, rather than accessing them with raw pointers. Here is an articifial example that explains the point: size_t size = 0; int * data = get_data_from_library(size); // raw data from library {5,3,2,1,4}, size gets filled in std::vector<int> v = ????; // pseudo vector to be used to access the raw data std::sort(v.begin