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];
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.
// 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); } }
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
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?
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.
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();
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.
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 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.
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?
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.
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.
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?
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.
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?
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)
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?
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;
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.