C/C++ :: Erasing / Removing Elements From A Vector?

Mar 23, 2015

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.

View 4 Replies


ADVERTISEMENT

C++ :: Searching STD Vector Of Strings - Counting Matching Elements And Erasing Them

Jul 19, 2013

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.

View 3 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++ :: Erasing Item From A Vector

Oct 28, 2014

Here is the code,

vector<int> v;
v.push_back(4);
v.push_back(11);
v.push_back(34);
v.push_back(5);
v.push_back(14);
v.push_back(32);
vector<int>::iterator next;

[Code] ....

I got a debug assertion error. What might be wrong?

View 8 Replies View Related

C :: Removing Duplicate Elements From Array

Nov 4, 2013

While removing duplicate elements from an array, if more than 4 array elements are same then removal does not work. however my logic seems to be alright. i reckon there is a problem with the conditions and assignments in the three for loops that i have used. the program is as follows:

Code:
/*c program to remove duplicate elements in an array*/
#include<stdio.h>
int main(void)
{
int array[30],i,j,k,n;
printf("

[Code] ....

Take for instance if the input elements are 1,1,1,1,1,2,2,3,5 then after removal of duplicacy the array is 1,1,2,3,5.

View 3 Replies View Related

C/C++ :: Removing A Name From A Vector

Nov 3, 2014

I'm supposed to create a program that stores names and then the final function is supposed to remove a name. But it only removes the first name instead of the inputted name.

the problem with names.erase(names.begin()+i)?

#include <iostream>
#include <string>//include for string function
#include <vector>//include for vectors

[Code].....

View 9 Replies View Related

C++ :: Removing From A Vector Of Sets?

Apr 18, 2014

I have a vector of sets in which I wish to remove a set from the vector, if it contains a certain value, is there any way of doing this?

for(int j = 0; j <= clauseVector.size(); ++j){
if(clauseVector[j].find(find) != clauseVector[j].end())
std::cout << "FOUND: " << propagator << "
";
}
}

This is what I have been using to find the element, but is there a way to remove the set that contains the element?

If needed I can include the full code

View 11 Replies View Related

C++ :: Removing Duplicate Strings In A Vector?

Sep 1, 2014

I'm trying to create a database/search engine program and I'm having remove duplicate strings from a vector. I'm mostly just trying to make it so that if 2 or more movie have the same title in the database, it will remove the duplicates and just print out one copy of the movie console. I tried using the unique() command, but it doesn't seem to work.

code:

#include <iostream>
#include <string>
#include <vector>

[Code].....

View 2 Replies View Related

C++ :: Removing Part From A Vector Of Sets?

Apr 18, 2014

I have a vector of sets, which I am removing any element which contains a certain value. For example, if I was looking for 2:

[0] 1 2 3
[1] 4 5 6

After the program was run, I would be left with just [0]4 5 6.

This is the code I have been using

auto iter = std::remove_if( clauseVector.begin(), clauseVector.end(),[propagator] ( const std::set<int>& i ){
return i.find(propagator) != i.end() ; } ) ;
clauseVector.erase( iter, clauseVector.end() ) ;

I want to know, is there any way I can tweak this code so that it only removes one part of the set rather than the whole thing. For example with above example, I would be left with

[0] 1 3
[1] 4 5 6

View 4 Replies View Related

C++ :: Filling A Vector With Elements?

Mar 21, 2013

I'm writing a program with a class containing a private std::vector<bool>. I chose bool because the vector represents a 2D array (think grid) and I only need 2 states per cell. I kept it one-dimensional as this hardly complicates things.

My problem is that I don't know how to initialize the vector, i.e. fill it with 0's.

The grid's resolution is not known at compile time, so I imagine I have to set the size (and content) of the vector in the class constructor.

Here's what I have tried among several things:

Code: World::World(const u_short worldsize)
{
grid.reserve(worldsize * worldsize); // grid is the private vector; square dimensions.
std::fill(grid.begin(), grid.end(), 0);
std::cout << grid.size();
} The output is 0. Only std::vector::push_back seems to have an effect on size(), but judging by its description, it doesn't look like the right candidate to populate a vector with zeros. Correct me if I'm wrong.

Frankly I expected line 3 to set the vector's size.

View 5 Replies View Related

C++ :: Swap Elements Of Vector

Apr 21, 2014

how to swap the first and 'mid' elements of a vector?

View 2 Replies View Related

C++ :: Generating All Possible Combinations Of Vector Elements

Feb 14, 2013

I have an integer vector of size "n".

e.g.
std::vector<int> vec;
vec.pushback(0);
vec.pushback(1);
vec.pushback(2);
vec.pushback(3);

Now I want to generate all possible combinations of size = {0, 1, 2, ... , n}.

{0, 1, 3} is not equal to {3, 1, 0} or {1, 0, 3} or {3, 0, 1}

View 7 Replies View Related

C++ :: Memory Size Of Vector Elements

Apr 9, 2014

I had a question about memory allocation/how iterators work for a std::vector<foo> of a user defined class 'foo'. Say foo contains variables of variable size, so that each member of the std::vector<foo> does not require the same amount of memory space.

Does c++ allocate the same amount of memory for each element, equal to the amount of memory required for the largest element? Or does it use some sort of array of pointers pointing to the location of each element in the vector to make the iterator work? Or does it use some other method? I am wondering because I wrote a code which reads data from a binary files and stores most of it in std::vectors.

The code seems to be using significantly more memory than the sum of the size of all the binary files, and I am using vectors made up of the datatype within the binary files (float). So I was wondering if internally the code was allocating space for each vector element which is the size of the largest element as a way to handle indexing/iterators. I ran my code through a memory leak checker and it found no errors.

View 16 Replies View Related

C++ :: How To Store Elements In 2D Vector And Call Them Out

May 5, 2013

I am currently trying to implement a 2d vector to store x and y of type int.

I have successfully passed it to the function, however i am unable to store the values in it. It always returns with a segmentation fault and my program terminates from there. May i know how do i store them properly and call them out?

Below is my code snippet

int reconstructSecret(int x, int y, vector< vector<int> > &inVec ,int constructSecret) {
int getX,getY,formula,accum,count,getSecret,startPosition,nextPosition,numerator,denominator;
getX=x;
getY=y;
int result;

[Code] .....

The main method

vector< vector<int> > inVec;
for(int i=0;i<constructSecret;i++) {
cout<<"please key in the "<<i<< "share of x value:";
cin>>x;

[Code] ...

View 9 Replies View Related

C++ :: Accessing Nested Elements Of Vector

May 1, 2015

so lets assume i have a nested vector in a set or vice versa o in a more general form a container in a container example...

Code:
std::set<vector<int> > my_tuple;

How do i access individual elements of lets say the first vector in the set! Or the last element in the 3rd vector?

View 7 Replies View Related

C++ ::  How To Print Out Elements Of Vector Of Type Pair

Oct 7, 2014

here is a piece of my code:

while(T--)
{
std::vector<std::pair<int, int> > *dragon;
std::vector<std::pair<int, int> > *villager;

[Code]....

I now wish to print the elements of the vector. How do I do it?

View 2 Replies View Related

C++ :: Vector Whose Elements Have Function Pointer Type

Mar 30, 2013

"Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type."

View 9 Replies View Related

C/C++ :: Insert Number Of Elements From One Into Another Vector Without Resizing

Dec 28, 2012

I think std::copy appears to do what I'm looking for.

I'm in need of a vector member function that would allow me to "insert" a number of elements from one vector into another vector without resizing or destroying either.

An example of what I'm wanting to do is assign the contents of two[3-5][50-54] to one[7-9][2-6]. Other than looping through both vectors using .at(), is there a way to copy this?

This would be continuous within a user controlled loop with differing elements being exchanged.

typedef vector<vector<unsigned char> > vec;  
vec one(10, vector<unsigned char>(10, '1')),
    two(90, vector<unsigned char>(90, '2'));  

View 4 Replies View Related

C++ :: Write Swap Function To Swap 2 Elements In Vector?

Nov 15, 2013

write a swap function to swap 2 elements in the vector?

View 3 Replies View Related

C++ :: List Of Vectors (vector Of N Vectors Of M Elements)

Mar 19, 2014

I create a list of vectors (a vector of n vectors of m elements).

std::vector <std::vector <int> > vsFA (n, std::vector<int>(n));

How I assign values? I try below, but not worked

void armazenaFA( std::vector <int> &vFA) // this function only knows about vFA
{ vsFA[n] [m]= simTime().dbl();
OR
vsFA[n].push_back(simTime().dbl());
}

View 1 Replies View Related

C++ :: Removing Element From Map?

Jun 20, 2013

I have a map as below. MoTopImpl is a class.

Map<MoTopImpl*,os_Reference_protected<MoTopImpl> > map_;

The map is populated as below:

void setMoInMap(MoTopImpl* mo,MoTopImpl* me) {
map_[mo] = me;
}

Now, I want to remove a specific element from this map. I am passing the element to be removed to the remove function as below:

void Asap::removeMoFromMap(MoTopImpl* mo) {
// First solution
if (mo != 0) {

[Code]....

And the function removeMoFromMap is called as below:

MoTopImpl* moTop = getMoTopImpl();
if (moTop != 0)
removeMoFromListMosSuspended(moTop);

But I am able to empty the map by iterating through the complete map as below:

Mapiter<MoTopImpl*,os_Reference_protected<MoTopImpl> > moIter(map_);
for (moIter = map_.first(); moIter; moIter.next()) {
moIter.remove();
}
cout << "Map zise = " << map_.size() << endl; // Prints zero

View 6 Replies View Related

C :: Removing Item From A Queue

Aug 17, 2014

I'm having a problem with removing an item from a queue. At first in the debugger I got SIGTRAP but I don't get it anymore but the problem still exists. When you try to remove an item from the queue the first nothing happens. Here's the code below compile it and you see what I'm talking about.

Code:

#include <stdio.h>
#include <stdlib.h>
struct Node {
char let;
struct Node *nextNode;
};

[code]....

View 12 Replies View Related

C++ :: Removing Characters From A String

Nov 1, 2013

I want to remove a particular character from a string. Say '.'. I've tried the following:

void removeDots(std::string &x)
{
std::remove_if(x.begin(),x.end(),[](char c){return (c == '.');});
}

This has the effect of removing the dots but keeping the length of the string the same, so old characters are left over at the end. Is there an extra action to do to x to make it the right size or is there a better way of doing it?

View 3 Replies View Related

C++ :: Removing Punctuations Off From A String

Jun 5, 2013

I'm working on a problem in which I've to design a program in which the punctuations should be removed from the string. For eg., if input str is: "Hello!!"; the output must be: "Hello".

I'm not sure how to remove a sub-string (if that's the right word!!) from a string. So, I designed a program which print out the punctuations. For eg., if input str is: "Hey!!"; the output would be: ! !

Here it is:

#include <iostream>
#include <string>
using namespace std;
int main (){
cout << "Enter a string" << endl;

[Code] ....

So, I want to know what should be added to this program so that the punctuations can be removed; or should I rewrite another program for that?

View 1 Replies View Related

C++ :: Removing All Non-doubles From A String

May 19, 2014

I have many random strings that look something like this:

" 55.343 Char 1.3825 asdf 0.1853 500 1.1359 4.0000 1 100 4.5043"

Notices how there are ints and chars and doubles in the string.

How do I remove all non-doubles for a string like this? The chars and ints may be anywhere within the string.

View 6 Replies View Related

C/C++ :: File I/O And Removing Duplicates

Nov 18, 2014

I have a program that's supposed to read in a file with comma seperated values. This file contains duplicates. The goal is to write a new file that does not contain any of the duplicates. I've successfully written the code to read in a file and create a new, identical file, but I'm failing at deleting the duplicates.

The format of each line in the file is: index,first_name,last_name,address,city,state,zip_code

Example: 1,John,Savage,579 Lone Street,Providence,RI,02903

As a requirement for the assignment, I've defined a Person class:

//File: Person.h
struct Person {
string index;
string first_name;
string last_name;

[Code] .....

This code writes the file I want (overlooking the duplicates) if I implement my equality operator as follows:

bool operator ==(const Person &a, const Person &B)/>
{
return false; //placeholder
}

Obviously this doesn't get the job done, since it will never detect a duplicate. The problem is that whenever I try to write any meaningful code, the program writes an empty file. The idea I've been trying to implement is to compare each of the members of Person like this:

bool operator ==(const Person &a, const Person &B)/>
{
//if two Person objects have equivalent names, they are duplicates
return ( (a.first_name == b.first_name) && (a.last_name == b.last_name) )
}

At first I thought the program was working just as before, but then deleting each line of the file as the result of an error in my code. However, I tried troubleshooting the problem by adding in

cout << a.last_name;

to parts of my code so I could see the value in certain places. Whenever I add this line or try to access a member of Person, the program writes a blank file.

View 3 Replies View Related







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