C++ :: Deleting A String Object In Vector?

Sep 21, 2013

Here's my code so far:

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

View 1 Replies


ADVERTISEMENT

C++ :: Deleting Object From A Container?

Nov 21, 2013

I have a container in c++11 and i must delete first object and i don't know how...

for (const prob& p : *static_cast<vector<prob>*>(list.listData)){
// i must delete the first object
}

View 2 Replies View Related

C++ :: Deleting Specific Class Object In Array

Jan 23, 2013

I am trying to delete a speific element in an array of class objects. i am overwriting the element i waant to delete with the element after it. My algorithm works but the output is not correct, after debugging it seems my objects just dont copy, is there a way to copy a class object, i have looked up copy constructors and attempted to write one but it does not seem to have any effect on the output.

below is my code

class user {
string firstname, lastname, currentteam, position, status ;
int age ;
public:
user() {};

[Code] .....

View 4 Replies View Related

Visual C++ :: MF Dialog App - Deleting HBITMAP Object

Mar 6, 2015

I have a little demo of a bitmap button that simply uses CButton and resource bitmaps in an MFC dialog app. The intent is to click on the button and have the bitmap image change. My code works ok the first time around, but crashes on repeat clicks. I have interpreted this as failure to properly delete the HBITMAP object, though I could be mistaken. In any case the app works ok in Release configuration but violates assertion in Debug.

Code:

ASSERT(m_hObject == NULL); // only attach once, detach on destroy[code]
// dialog header
// Attributes
protected:
HBITMAP m_hBitmap1;
HBITMAP m_hBitmap2;

[Code] .....

View 9 Replies View Related

C++ :: SDL Deleting From Vector

Apr 13, 2013

Simple rect collision detection

bool check_collision(SDL_Rect *box1, SDL_Rect *box2){
if(box1->x+box1->w<=box2->x)return false;
if(box1->x>=box2->x+box2->w)return false;
if(box1->y+box1->h<=box2->y)return false;
if(box1->y>=box2->y+box2->h)return false;

return true;

[Code] ...

The order of the game is of course standard.

1.handle input
2. handle logic
3. render
4. update

It's all pretty simple and straight forward, but oftentimes (not always) when the weapon hits the asteroid, the program crashes. If for example I turn off the delete main_weapon[i] and main_weapon.erase(main_weapon.begin()+i); the program works perfectly, but of course the bullets go right though the asteroid object, which is not what I want. It's only the weapon deletion which is causing no end of trouble.

View 7 Replies View Related

C++ ::  Deleting Vector Of Pointers?

Nov 23, 2013

Currently I am implementing the A* algorithm in C++. I have chosen to use a hybrid of a '2D vector grid' and two 1D pointer vectors to specific places in the '2D vector grid'. I have chosen to do it this way, so I can access the nodes using coordinates and also for iterating over the appropriate nodes(rather than the whole '2D vector grid').

In the below examples I have not included the full code because I deemed it irrelevant to the question.

vector <int> CInGame::AStarAlgorithm(vector<vector<bool>>& resistanceMap, int startX, int startY, int targetX, int targetY, int cutOff) {
vector <int> returnVec;
vector <vector<CNode>> twoDimNodes;
vector <CNode*> openSet;
vector <CNode*> closedSet;

[code].....

The error is:

_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

do I need to free the pointers or is it done automatically? If not then how would I do this?

View 3 Replies View Related

C++ :: Deleting Vector Of Pointers?

Oct 14, 2014

I made a vector of pointers and the problem is that I have trouble deleting the pointers in the vector. I used to simply do vector.clear() or vector.erase() however it does not clear the actual memory. And I tried something like this:

std::vector<Foo*> Vector;
std::vector<Foo*>::iterator i;
for (i = Vector.begin(); i < Vector.end(); i++)
delete *i;

View 5 Replies View Related

C++ :: Repeat Found - Deleting In A Vector

Oct 12, 2013

I created a very basic program which contains a vector (my vector) that holds 0, 1 and 1. This program compares each element in the vector. If the element after the one currently being compared is equal to it, it outputs "repeat found!" Now this all works perfectly, but my next step was to erase the repeated one. Everything was working up until the delete step. I get a weird error that says "vector iterator not dereferencable" .

// vector::begin/end
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> myvector;

[Code] ....

View 6 Replies View Related

C++ :: Dynamically Deleting Items Out Of Vector

Aug 31, 2014

I have the following code in which I wish to dynamically delete items out of a vector based on the condition of the item in the vector.

The below code works but throws the error: "vector iterator not incrementable" at certain times.

It seems like this happens when the last item in the vector get's deleted. How do I solve this problem?

for (std::vector<PhysicsObject*>::iterator it = CurrentBoxes.begin(); it != CurrentBoxes.end();/*it++*/) {
(*it)->CalculatePhysicsOrientation();
(*it)->DrawMe();
glm::vec3* CurrentBoxPosition = (*it)->GetCurrentPosition();
if (CurrentBoxPosition->y < -750.0f) {
it = CurrentBoxes.erase(it);
}
++it;
}

View 9 Replies View Related

C++ :: Deleting Vector With Char - Segment Fault

Apr 19, 2012

The below code is giving me segment fault during delete [] (*iter) sometimes. Is it not the proper way to delete the vector with char *

Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<char*> nameList;
for (int i=0;i<1000;++i)

[Code] ....

View 7 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

Jun 7, 2012

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.

View 9 Replies View Related

C++ :: Sorting Of Object In A Vector

Oct 24, 2013

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..

original
X: 4 Y: 5 CIV: 10
X: 3 Y: 2 CIV: 30
X: 3 Y: 3 CIV: 05

sorted
X: 3 Y: 2 CIV: 30
X: 4 Y: 5 CIV: 10
X: 3 Y: 3 CIV: 05

missionplan.cpp
void MissionPlan::topfives() {
stable_sort (topfive.begin(), topfive.end());
pointtwoD = topfive.at(i);
pointtwoD.displayPointdata();
}

View 2 Replies View Related

C++ :: Erasing Object From Vector

Apr 4, 2012

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.

View 8 Replies View Related

C++ :: Testing Object Against All Other Objects In Vector?

Jan 28, 2015

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?

View 11 Replies View Related

C++ :: How To Initialize Vector In A Class Object

Mar 6, 2013

I want to make a class, named generation, having a vector<chromosome> type data member.

chromosome is also class name.

chromosome has a 'route' as data member, which type is class TArrayI defined in ROOT package.

the size of route is to be fixed according to parameters of generation class object.

class chromosome{
public: chromosome(){;}
....
protected:
TArrayI route;
};
class generation{
public: generation(int PopSize=300, int numCity = 36){
.........
}
vector<chromosome> Population;
};

I want each element, chromosome of Population, to have a numCity size of route.

View 2 Replies View Related

C++ :: Looping Through A Vector Of Object Pointers

Sep 4, 2014

Lets say for example I have the following vector:

vector<Component*> mComponents;

and a function print() inside of class Component that prints the objects name.

How do I loop/iterate through the vector to access the print function in each object.

View 1 Replies View Related

C++ :: Find Object In Vector And Delete?

Dec 14, 2014

I have this class I have been working with,

Code:

// 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);
}
}

View 4 Replies View Related

C/C++ :: How To Create A Vector In Header File That CPP Object Can Use

Apr 25, 2015

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 >)

[Code] ....

View 9 Replies View Related

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

Aug 12, 2013

I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code

I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?

Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class

[Code] ...

Here is the lines of code i want to understand.

Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);

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.

View 3 Replies View Related

C/C++ :: Comparing Input String To Type String Vector

May 29, 2014

I wrote a program that reads a list from a file and stores it in a string type vector. Now, I want the user to input a word so that the program can search the vector to see if that word already exists. I have used every possible way of reading input from the console and storing it in order to compare with the vector but it never results in a match. When I print the input string and the vector string they are exactly the same thing (or at least print to the console as if they were). I've tried using getline; using cin direct to a string var; using cin to a char array and then casting to string using string str(arr); I even added a newline at the end just in case and STILL I cannot get a match.

vector <string> currentSet; //read a list in from a file and has 9 items in it
cin.ignore();
string line;
getline(cin, line);
if(line == vector[0]){//if printed to console line is HEAT and vector[0] is HEAT
cout<<"match"<<endl;
}

View 3 Replies View Related

C/C++ :: Length Of String Object?

May 7, 2014

consider the following code:

string input;  
getline( std::cin, input )

In the code above, what is the maximum number of characters that can be read into variable input?

View 4 Replies View Related

C++ :: How To Use A String To Create Class Object

Dec 24, 2014

I wanted to create a new class object and I want to name it from a string. Something like this:

string name = "Mike";
Customers name;
//to create a new object in customer class using the name string

Is there any way to do this?

View 11 Replies View Related

C++ :: How To Give Fraction Object In S String Via Template

May 9, 2014

I am trying to give fraction object in s string via template.

struct Fr // simple Fraction {
int num;
int denom;
Fr(int i,int j) {

[Code] ....

View 10 Replies View Related

C++ ::  program That Reads Into String Object A Name With Three Blanks Between First And Last Names

Oct 4, 2014

" Write a program that reads into a string object a name with three blanks between the first and last names. Then extract the first and last names and store them in separate string objects. Write a function subprogram that displays its string argument two times. Call this function display the first name for times and then to display the last name six times."

I completed coding the first part of this task with the strings and combining strings together. However, i have having trouble with the other half of this task. It begins from "Write a function subprogram to display the last name six times." This is where i have so far --

#include <iostream>
#include <string>
using namespace std;
int main()
{
// Defining the strings

string firstname, lastname;
string wholename;
string greet = " Hi ";

[code]....

View 2 Replies View Related

C++ :: How To Assign Char Array As User Defined String Object

Apr 17, 2014

STRING s1 = “FOOBAR”

Here STRING is a user defined class. how to assign a constant char array "FOOBAR" to string object? Copy constructor need to be same class type as parameter. and overloading assignment operator also need to be same class type. I think 'friend' can let pass another type of object rather than STRING to do operation on overloaded '=' operator. How could it be done if it is possible at all?

View 2 Replies View Related

C++ :: String Stream Object And Text File Data Access

Jan 21, 2014

I have two puzzling issues I am dealing with.

Issue 1: I am using a stringstream object in a block of my program that needs to be visited repeatedly depending on a user's selection from a menu. I want the contents of this stringstream object to be cleared any time control gets to this part of the program. I have tried the clear and flush functions to no avail.

Issue 2: I am reading data from a source text file that would be regularly changed during the course of program run. After the program run is over, I am supposed to save the results(which is basically the source text file AND all updates) in a destination file. This destination file would then serve as the source file when next the program is run. In other words, I want a scenario where my results overwrite the original contents of the source file; implying that my source and destination files are now one, pretty much. How can I do this?

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved