I am erasing a object from the vector. I want to know that do i need to delete the object before I call the erase method or calling the vector erase method is suffice. See the following example.
Class A {
int val;
};
Class B {
vector<A *> vectorA;
void AddA(int val) {
A *a = new A;
a->val = val;
vectorA.push_back(a);
[Code] ....
I don't know which DeleteVectorEntry to use which makes sure that there is no memory leak in my program.
I am working on a project for class where I use a parent Shape class with circle, rectangle, ect. Classes inheriting from that. Along side these I use a class called Scene. In main I need to create a scene and add some shapes to a vector in scene.
vector<Shape*> shapes
I need to use functions addShape(Shape* shape) and a delete shape method. I have the addShape finished. The problem I am having is with the delete method. Is there a way that I can use something like deleteShape(Shape* shape)? Is it possible for me to delete a specific shape from the vector by passing in that shape, or can it only be done using index? I have looked at the documentation for std::vector as well as std::vector::erase. I am wondering this because if I use index values and do something like
void Scene::deleteShape(unsigned int x) { shapes.erase(shapes.begin() + x ); }
It will lead to some errors later on due the the changing size and indexes of the vector and elements.
I have read that the Erase-remove idiom is the way to go. I have a rough understanding of how this works but am unsure whether I can implement a match-counter along with it.
For counting alone, I would use something like this:
Code: std::vector<std::string> v; // contains duplicate strings in different elements std::string term = "foo"; // search term, changing at runtime as well
unsigned int matches = 0; for( auto e : v ) { if( e == term ) {
[Code] .....
I'm not sure how (or if) I can combine the two things. That is, I don't know how to integrate a function comparing two (changing) strings into the remove_if() method. Nor do I know how to increment a counter during iteration.
The vector is extremely large, so speed is paramount. I think there are many other avenues for optimization, but decreasing the vector's size for each consecutive search could deliver a big speed boost for subsequent searches I imagine, as traversing it holds the biggest cost.
I'm new to C++ especially vectors.. I've a question regarding sorting of object in a vector.. My object consists of x,y and civ. so I would like to know how do I sort the value of the civ in an descending order but at the same time retaining the value of x and y attached to it..
I'm working on collision detection for a game in SFML. I successfully designed a Spatial Partition grid to speed up the collision test, in the following of this tutorial: [URL] ....
But now I have an issue with one aspect of it: Going through a vector of objects and testing all the OTHER objects in the vector against said object. The author puts it into psueudocode here:
For each tick of the clock
For every object in the game
Get all the other objects in the same grid square
For each other object in the same grid square
I have trouble with the last line, because in iterating through a vector I am not sure how to skip over the current object. Here is my own code (a couple of sysntax errors but this is a c++ question not an SFML question):
//for every moveable object for(int i = 0; i < rects_.size(); i++){ std::vector<sf::RectangleShape> posibleObjects_; //this will be a vector of WorldObjects in a real game //for every object in that object's gridsquare for(int j = 0; j < rects_.size(); j++){ if(rects_[i].intersects(rects_[j])){ //collision } } }
The problem is, a collision will always be reported because somewhere in the vector the object will eventually check against itself which is always a true collision. What is the correct way to do this?
case DELETE_TITLE: std::cout << "Game to remove from list: "; std::cin.ignore(); getline(std::cin, gameTitle); for (iter = gameList.begin(); iter != gameList.end(); ++iter) {
[code]....
It deletes the text from the string but not the index it self.
It deletes the text but when I print the list of game titles it has it there blank. I want it to completely remove the object from the vector from where it was deleted
// objects to hold results, row id, and name class result_holder { public: // initialize class members result_holder() : row_id(0), row_value(0.0), row_name("") { }
[Code] ....
There are cases where I need to find an object based on the value of row_id and delete the object from the vector row_results. I could find the proper object by looping through the vector and testing against each member.
Code: // id I am looking for unsigned int id_to_delete = 12; for(i=0; i<row_results.size(); i++) { if(id_to_delete == row_results[i].row_id) { delete row_results[i]; } }
I have used find before to find the position in a vector with a specific value, but I don't know how to use find to locate a specific value for an object member.
Also, is delete what I need to get rid of the object or should I be using erase as in,
Code: // id I am looking for unsigned int id_to_delete = 12; for(i=0; i<row_results.size(); i++) { if(id_to_delete == row_results[i].row_id) { row_results.erase(row_results.begin()+i); } }
I'm working on a grocery store inventory project. One part is to have a shopping cart, where customers can put in up to 20 items. Because there can be up to 20 shopping carts at one time, I want to use a vector inside the cart object to represent all the individual food items.
Here's my code,
Header:
#ifndef CART_H #define CART_H #include <vector> class Cart { public: Cart(); Cart(std::vector< int >, std::vector< int >)
I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.
will copy constructor does object initialization using another already created object? I understand that it can be applied for object initialization and not for assignment.Is it correct?
I have a method to take a Tile object and make an instances of it based on some data from the original object. Than it is suppose to manipulate the a specific instance and save the results. The first loop through it works but it changes all instance as well as the base.
public static int recurse(int count, Tile[,] b,Huristic h,int check) { if (check==1) { boardState.Add(B)/>; return check; } if (check == 0)
this function will return a temporary integer now void fun1(const int & num); this function can receive from myfun().BUT void fun2(int & num); this function cannot receive from myfun() Why is that, Moreover what is lifetime of a temporary object like one returned in myfun() ???
I am using the above code to retrive an item selected by user,But this line is giving an exception "Null Reference Exception, Object reference not set to an instance of an object"
In Visual Studio 2010 C++ I have a series of existing text objects The text properties names are item1_lbl, item2_lbl, item3_lbl, ….
Based on a selection I want to change an object. I generate the name of the object I want to change in a string so from this string is there a way to get a pointer to the correct text object that is same name?
How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.
here is my code:
#include <iostream> #include <array> #include <vector> using namespace std; int duplicate( vector < int > &vector1, const int value, const int counter)
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code: // opens mol file, reads in rows to string vector and returns vector vector<string> get_mol_file(string& filePath) { vector<string> mol_file; string new_mol_line; // create an input stream and open the mol file ifstream read_mol_input; read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
I'm trying to figure out how I can create a vertex array object at offset of a vertex buffer object.I've created the buffer object. I'd like the "texs" idnex data to start at the texture coordinate content of the vertex_t structure.
Type definitions:
struct vertex_t { vector3d_t position; float s; // Texture coordinate s float t; // Texture coordinate t };
Code so far:
// How do I make this start at a certain spot of the VBO?! glVertexAttribPointer(texs, 2, GL_FLOAT, GL_FALSE, sizeof(vector3d_t), nullptr; //...
I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.
For instance, here is a test program that successfully finds the maximum according to just one member of the structure:
And the output was this, as expected: Maximum element S.a of vector<S> vec is at: 9 [I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6 [I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7 [I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8 [I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9
However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.
This was the description of the std::find function that I found: find - C++ Reference
Code: template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference
Code: template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:
Code: int x=7; std::vector<S>::iterator result; result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;
Is there another std function that I can use to make search and find an element according to just one member of myStruct?
Or perhaps there is a clever way to pass two arguments to the unary predicate.