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


ADVERTISEMENT

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 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 :: Reading Matching Strings From A File And Deleting From Another

Sep 26, 2014

We are generating emails for freshman students, however, the system probably due to server overload fails to generate some emails. So I'm working on this c code( probably was a wrong choice) to search for regexp matching the generated emails and deleting from the list of students and propective emails.

I"m currently at the stage of making sure the code can find all the matching regexp from the student list file before deleting! But the code fails to read all matching regexp?

Code:
//Filename: SearchReplace.c
//Usage: This searches for lines in a file containing a particular work and deletes the lines.
//Licence: GNU P.L.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

[code] .....

What I'm missing. It recognizes some regexp and fails to recognise some. Been staring for hours!

View 4 Replies View Related

C++ :: Counting And Summing The Elements?

Nov 21, 2013

Write a program in C/C++ that counts and summing the elements (numbers) from the given interval in the array.

View 3 Replies View Related

C++ :: Counting Of Elements In A List Of Lists

May 1, 2013

I have a problem like this: I do not know how to count the number of elements in a list of lists. I have the following list of list.

#define VAR(V, init) __typeof(init) V=(init)
#define FOR_EACH(I,C) for(VAR(I, (C).begin()), ite = (C).end();
(I) != ite;
++(I))

std::vector<std::vector<GLdouble> > contours;

[Code] ....

I do not know how to write the part indicated in Bold that represents the number of elements of contour. As seen from the code used for the tesselation OpenGL.

View 3 Replies View Related

C++ :: Counting Elements Of Constant Array?

Aug 21, 2014

This is going to seem like a stupid question but how can I count the elements of a const array. Surely c++ compilers provide a count for const arrays?

const wchar_t* ItemHandler::itemNames[] = {L"Coins", L"Matches"};
const wchar_t* ItemHandler::itemIconLocations[] = {L"./media/Items/coins.png" , L"./media/Items/matches.png"};
const bool ItemHandler::itemStackable[] = {1, 1};

I want to count the elements so if the server sends a bad item id it won't crash every client in range lol. I heard that the sizeof keyword returns the size of the array in bytes. I used to think the size of keyword would return the element count but found out it isn't.

View 2 Replies View Related

C++ :: Nested Vector Initialization - No Matching Function For Call

Jun 26, 2013

I initialized a nested vector with the following code as:

Code:
vector<vector<Point> > vectorB(4, vector<Point> (int(vectorA.size()), 0));

And came across the following error during link stage:
"/usr/include/c++/4.6/bits/stl_vector.h:1080:4: error: no matching function for call to ‘std::vector<cv::Point_<int> >::_M_fill_initialize(std::vector<cv::Point_<int> >::size_type, int&)’ "

View 6 Replies View Related

C++ :: STRINGS - Counting Words With WHILE LOOPS

Sep 29, 2013

I need to count Words in a string in <stdio.h> and <string.h> format ONLY, must be in WHILE Loop! Our professor acts like were programming students when were just marketing students

View 2 Replies View Related

C++ :: Searching Vector With Maps?

Sep 19, 2014

Currently im creating a simple phone directory. I am having a problem when searching the vector. It only lets me search for the exact key in the directory when I need to to search for strings that are close to the name. For example when I search "car" it will state that its not in the directory when it should pull up Carr, Derek and Carr David.

#include <string>
#include <map>
#include <vector>
#include <ostream>
#include <iostream>
int main() {
std::map<std::string, std::vector <std::string> > directory;

[code]....

View 1 Replies View Related

C++ :: Generic Class Array Based On Counting Access To Elements

Oct 3, 2013

I was writing generic class Array (based on counting access to elements)and i got compiling error I cannot even understand (in vs2012).

Code: [URL] ....
Error: [URL] ....

View 8 Replies View Related

C++ :: Searching String Within Vector Struct

Nov 3, 2013

New to C++ lambda, got two question in regards of searching for strings within a vector struct.

Minimalistic example:
Code:
struct singleFile {
std::string filename;
};

std::vector<singleFile>myStorage;
myStorage.push_back(singleFile());

[Code] ....

1) why does found always return 0 eventhough the filename exists?

2) I don't know how to pass an actual search string instead of the hardcoded approach as above with filename =="

View 6 Replies View Related

C++ :: Searching A Vector Of Objects For Pairs

May 16, 2013

Say I have a vector of objects and I want to return an object based on a pair of strings. The strings can be in either order Ie; A B==B A.

In general, what do you think is the best way to do this?

View 5 Replies View Related

Visual C++ :: Vector Input / Counting Words

Feb 3, 2014

I am trying to write a program that counts specific words that a user inputs "Howdy/howdy/Whoop/whoop" (yes I go to Texas A&M, hence those specific words) I am having an issue where it wont count the first word even if it is "Howdy"

For example if I put it:

"Howdy howdy whoop Whoop" - it only outputs that it counted 3 words

now if I were to do:

"Hello Howdy howdy whoop Whoop" - it would count 4.

Code:

#include "std_lib_facilities_4.h"
int main(){
cout << "Please enter desired words, when you have entered all words, please type CTRL+d (EOF Command)
" << endl;
//It was assumed that EOF command was going to be used here hence the necessity of 'CTRL+d'
vector<string>words;

[Code] ...

View 2 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++ :: 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++ :: Vector With Strings And Integers

Apr 5, 2013

I have a file where the first column shows letters, second column shows numbers.

How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?

View 13 Replies View Related







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