C++ :: Vector Delete Not Functioning

Dec 1, 2013

My delete is not working. Why?

Code:
struct MatchName{
MatchName(string& searchName) : s_(searchName) {}
bool operator()(const clsStudent* student) const {
return student->getName() == s_;

[Code] .....

View 4 Replies


ADVERTISEMENT

C++ :: Variable Not Functioning Properly

Sep 29, 2014

#include <iostream>
using namespace std;
int main()
{

[Code]...

For some reason, I get the cout of

321
1

Because of the a++, shouldn't it be

321
2

The second example is what the cout needs to be.

View 1 Replies View Related

C++ :: How To Delete The Objects Within A Vector

Nov 10, 2013

here's the problem. I want to delete the objects within a vector, although I'm not sure whether I should clear the vector afterwards. Is it really necessary?

Code:

for (i = 0; i < allSales.size(); i++)
delete allSales[i];

allSales.clear(); //Is this step necessary?

View 5 Replies View Related

C++ :: Delete Pointers From Vector

Jul 25, 2013

I have something like this:

class A {
};

class B : public A {
};

class C : public A {
};

B*b1;
B*b2;
C*c1;
C*c2;

vector<A*>vec;

int main() {
vec.push_back(b1);
vec.push_back(b2);

[Code] ....

And it don't works at all. all i get (when playing with variations of this stuff until it compiles correctlly) is a memory leak.

For example, let say i have b1 address = 1234

I will effectively free the memory at 1234, but for a strange reason, the memory leak is elsewhere, for example, at 2345, and the memory value at this address is equal to... 1234, the address of the pointer i wanted to delete.

View 7 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++ :: Bubble Search / Vector Delete

Apr 23, 2012

Just doing some work where i need to produce a database in C++ for DVD now I have done most of it but I'm stuck on some bits. I have split the database up into different files but I will post the files which are important. How to do a search function. I got told it's called "Bubble search" and then a delete function which i think is called "Vector delete".

My header file

Code:
#ifndef DVD_DB_H
#define DVD_DB_H
#include "dvd.h"
#include <vector>
class dvdDB {
private:
std::vector<DVD> dvds; // A container that contains an arry of DVDs

[Code] .....

View 1 Replies View Related

C++ :: Sequence Of Actions - Delete Pointer In Vector

Jul 29, 2013

I'm tying to create a program that evaluates all possible actions for a certain problem.

So what i'm basically trying to do is to create a sequence of actions, evaluate them to check if it's the best sequence, change the sequence, evaluate again and so on until all possible scenarios are exhausted, leaving the best one in the end.

My approach to this at first was to create a tree of all possible options and then evaluate each branch. Since there are a lot of possible cases i ran out of memory while the program was still creating the tree. I changed this to create just a branch, evaluate it and then modify it.

I was still getting memory problems. I declared a class tNode and declared a vector<tNode*> branch. Then i created all the nodes i needed for that branch with branch.push_back( new tNode() ). When i wanted to modify the branch i simply used branch.pop_back() and again a branch.push_back( new tNode() ). I figured i was getting the problem because although i clear the vector, i don't actually clear the reference in memory. Is this correct? If so, how can i actually delete the memory space and not just the pointer in the vector?

View 6 Replies View Related

C++ :: Delete Member Of Vector That Is Pointed By Pointer?

Apr 17, 2013

I have following:

struct Point {int* a; int b;};
vector<vector<Point> > numbers;
vector<int> example;

The numbers vector has a matrix of a sort and each of the members are pointing to one member in the example vector. A member numbers.at(2).at(3).a is pointing at example.at(3). Now, can I remotely delete a member in the example vector using the pointers? Like so:

delete (*(numbers.at(2).at(3).a));

I know there is a more convenient way to delete members, but this is a very specific case I'm working on.

View 3 Replies View Related

C++ :: Vector Of Int Class - How To Delete Pointer X In Destructor To Free Memory

Feb 25, 2015

An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.

#include<iostream>
using namespace std;
class vectorOfint{
int* x;
int size;
public:
vectorOfint();

[Code] .....

View 6 Replies View Related

C :: List - Why Delete Function Doesn't Delete

Dec 9, 2014

I have to manage a Clinic. I need to delete a booking (for example, if John said he's coming on March 22nd at 15:30 but then he say he's not going to come, I have to delete that booking so another person can use it).

idSearched: the id of the person that is not going to come. I have a lot of specialties and each one has a list. So I ask for the speciality to delete the node (the node contains John's booking). If I don't find it, I return (-1). searchSpecByID return a pointer to the list where the speciality is. So head will point to the first node of the list. In nodeToDelete I have the node I want to delete.

The program detects OK when is the first in the list and when not, but it doesn't delete the node.

Code:

typedef struct bookingList{
tSpecialty specialty;
struct nodo* intro;
} tList;

[Code].....

View 7 Replies View Related

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

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)

[Code].....

View 3 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++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related

C++ :: Find Function To A Vector Of Structures Vector

Jul 5, 2013

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:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

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.

View 4 Replies View Related

C/C++ :: Cannot Print Vector Of Vector Of Doubles

Mar 31, 2014

I am trying to print a matrix solution that I've stored in a vector of doubles. The original matrix was stored in a vector of vector of doubles. I want to print to the screen and an output file. Right now it just prints to screen column-style and the output file is horizontal-style. I tried to change the solution from vector of doubles to a vector of vector of doubles like the original matrix but when I run my code it crashes. Here is what I am referring to:

void readMatrix(vector<vector<double>> &matrix, vector<double> &b, int &N, string input) {
ifstream in(input);
in >> N;
matrix.resize(N);
b.resize(N);

[Code] ....

However when I change my printResult function to this (I removed the string argument because I just want to get it working to the screen first):

void printResult(vector<vector<double>> &sol, const int N) {
//ofstream out(output);
//int j;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){

[Code] ....

The program crashes. Am I even printing the matrix correctly?

View 4 Replies View Related

C/C++ :: Swapping Vector Int And Vector Strings

Mar 30, 2013

I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they walk away.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>  
using namespace std;  
    struct InventoryItem

[Code] ....

View 4 Replies View Related

C++ :: Can't Read In Matrix Into Vector Of Vector Of Int

Mar 24, 2012

Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>

[Code]....

Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.

View 14 Replies View Related

C++ :: Copy Part Of Vector 1 To Vector 2

Jun 21, 2012

I am trying to copy vector to vector as follow:

Code:

std::vector<unsigned char> vec1;
//insert some values into vec1
std::vector<unsigned char> vec2;

Now I want to to copy 2 bytes from vec1 starting at index 5., why do i need to know how many bytes from the end of vec1?? can't i just specify how many bytes i want to copy from starting index?

std::copy(vec1.begin()+5, vec1.end()-??, vec2.begin());

View 2 Replies View Related

C++ :: How To Use New And Delete Keywords

Jul 25, 2013

I am beginner with C++ and I want to know. When do I use new and delete keywords? And what can I use them to do? Only thing I know about the new keyword and delete is that you can do this:

Code:
int Size = 5;
int *ptr = new int(Size);
cout << *ptr << endl;
delete ptr; And more . . .

But when do you use this keywords? I am just curios about them and want to now if them can be really effective to use.

View 6 Replies View Related

C++ :: Cannot Delete Object

Apr 18, 2014

int NextPixel(int antIndex, int currentPixel , Ant *ant , IplImage *edgeImg) {
int left = currentPixel - 1 ;
int right = currentPixel + 1 ;
int up = currentPixel - edgeImg->widthStep ;
int down = currentPixel + edgeImg->widthStep ;

[[Code] ....

if those lines are uncommented , program gives heap error.Why?

View 3 Replies View Related

C# :: Delete A Row From DataGridView

Jun 11, 2014

How would I delete a row in a datagridview (and in the database) if the datagridview control does not show the primary key? ie the SELECT statement we used to load the DataGridView does not include the primary key column (since it is not relevant to the user)

View 14 Replies View Related

C++ :: Delete All Parent Methods Of A Given Name?

May 7, 2013

I'm writing a sorted vector implementation and trying to do it as simply as possible.

Right now, I'm declaring the sorted vector with a protected subclass of vector, then using the "using" keyword to explicitly inherit all methods that aren't related to adding new elements to the vector (so I can control the order).

Eg:

Code: template<typename T, class Cmp = std::less<T>>
class sorted_vector : protected std::vector<T>{
public:
typedef typename std::vector<T>::iterator;
using std::vector<T>::operator=;
using std::vector<T>::assign;
using std::vector<T>::get_allocator;
using std::vector<T>::at;
using std::vector<T>::operator[];
//...
};

Obviously, this is annoyingly redundant. So what I'd like to do is something using the new "delete" keyword from C++11. Is there any quick, expressive way of deleting specific methods?

Also, it's pretty annoying to have to typedef base_class::type type to inherit a type from a base class. Is there a shorter way to do that?

View 14 Replies View Related

C++ :: How To Delete A Parameter In A Function

Apr 17, 2014

How I can delete a parameter in a function .

int *buildTrail(int antIndex, int start, double *pheromones) {
int *trail = new int[tabu];
bool *visited = new bool[tabu];
trail[0] = start;
visited[start] = true;

[Code] ....

If I comment all lines includes visited word , no exception occurs , Otherwise , exception throws.

Simply put , How can i delete visited parameter as long as its role has been finished?
.
.
.
delete visited ;
return trail;

View 4 Replies View Related

C++ :: Check If Can Do Delete Pointer

Jan 4, 2015

void myfuncion(){
int *a = new int[10];
int b[10];
int *p = b;
delete a
delete p
}

I cant delete b and/or p but how can i check it if i can use delete or not?

i want to check if the pointer is pointing on function temp variable ( those what gets deleted after function ends .

View 5 Replies View Related

C++ :: How To Delete Element From Array

Jan 9, 2015

how to delete an element(s) from an array; suppose I have an array x[10] = {1,2,3,4,5,6,7,8,9,10}, and I want to delete array{5} so that the values of the array become {1,2,3,4,5,7,8,9,10}; how do I go about this? This is not the same as setting the value of array{5} to null; but completely eliminating it so that it does not get printed alongside the other elements of the screen.

View 3 Replies View Related







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