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


ADVERTISEMENT

C :: Searching Array Of Struct Students

Feb 28, 2013

I have to search an array of struct students on the basis of their first names, last names and roll numbers .. so far i am done with the input of the student first , last names and roll numbers . I have problem in searching the array ..

Code:
#include <iostream> Code: #include <string>
using namespace std;
struct Student {
string FirstName ;
string LastName ;

[Code] ....

View 5 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++ :: 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

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++ :: Searching Files For A String

Jan 30, 2013

I have 80,675 Files, 8,213 SubFolders.

I would like to search each file for a string, which will be last name.

Matches string, outputs path/filename, either to screen or a output file.

I have done this to a single file but never to multiple files and folders.

View 2 Replies View Related

C++ :: Searching For Character In String

Jun 19, 2013

I'm trying to find a < character in a document, get it's position. Then find > and get it's position. Then i want to delete all things between that but runtime is terminating my process so i don't know what to do.

The code:

#include <iostream>
#include <conio.h>
#include <stdio.h>

[Code].....

View 6 Replies View Related

C :: Searching A String On Char Matrix

Apr 12, 2014

I want to make a program that receives the number of lines and collumns of a matrix, a matrix of char, the number of pairs of coordinates and the coordinates. the program has to return the char (or chars) that are on those coordinates on the matrix.

Example:

receives
2 3 ABC DEF 2 1 1 1 2

returns

AB

this is how i tried to solve this problem:

Code:
#include
#define MAX 1000
int main() {
int nlin, ncol;
char mat[MAX][MAX];
int x[MAX], y[MAX];
int ncoords;
int l, c, n;

/* receiving variables and storing matrix.*/

[Code] .....

For some reason that i can't seem to find, this solution is not right.

View 6 Replies View Related

C/C++ :: Sorting And Searching Two Arrays (int / String)

Apr 2, 2014

Write a function to read and display the contents of names and marks. You then ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student.

Next, sort the arrays, write them out and then ask the user for a name to search for. This time use the binarySearch to return -1 or an index. Display the student's name and mark if found.

void getNames(ifstream& inStream, string names[], int marks[], int numElts);
int linearSearch(const string names[], int numElts,string who);
int binarySearch(const string names[], int numElts,string who);
void selectionSort(string names[], int marks[],int numElts);
void displayData(const string names[], const int marks[], int numElts);
[Code] ....

Now I have worked up some stuff in parts but I am so lost and confused with these specific requirements: Previous questions asked me to sort out a linear search, a binary search and

LINEAR SEARCH:

int searchList(int list[], int numElems, int value) {
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if value was found

[Code] ....

View 3 Replies View Related

Visual C++ :: Searching File For String?

Nov 8, 2012

The program then asks the user for a search string, which may contain white space. The program should search the file for every occurrence of the search string. When the string is found, the line that contains it should be displayed in the following format

nnnnn:Line Contents

That is the line number of the line, 5 columns wide, right justified, followed by a colon, followed by the contents of the line.

And this is what I've got so far:

Code:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int main(int argc, char *argv[]) {
ifstream inFile;
string fileName,

[code]....

But this doesn't work. It prints everything in the file, not just the lines where the string is found.

View 9 Replies View Related

C++ :: Searching Through Entire String Looking For Spaces / Punctuation

Dec 11, 2014

I have it searching through the entire string letter by letter, looking for spaces, punctuation, etc... yet it still is continuing on with the space.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdio.h>
#include <cctype>
#include <algorithm>

[Code] ....

Output:
if(str_word == " ")
//or
if(str_word == ' ')

It does nothing to change it. I am completely baffled.

View 4 Replies View Related

C++ :: Syntax To Use vector Within Struct?

Apr 9, 2014

What is the proper syntax to use a vector within a struct? Here's an example of what I want to do:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct myStructure {
float number;
vector<string> nameList();

[Code] ....

There must be some way to clear that vector. Why this doesn't work?

View 2 Replies View Related

C++ :: Iterator To A Vector Of Struct Type?

Nov 18, 2013

I'm working on a program where I have a vector full of <myClassType> structs.

I'm trying to insert items into a vector, searching first through the vector to make sure the value isn't already in the vector before inserting it. The "find" function isn't working properly.

I keep getting C2678 "binary '==': no operator found which takes a left-hand operand of type "myClassType" or there is no conversion errors in Visual Studio 2010.

I know it's something having to do with the find function and my iterators, but I can't, for the life of me, figure out what it is.

I've tried switching to const_iterators, but I get the same error message.

void foo::insert(const myClassType& en) {
vector<myClassType>::iterator it;
vector<myClassType>::iterator it1;
it = find(Table.begin(), Table.end(), en.memberOne);

[Code] .....

View 3 Replies View Related

C++ :: Print Values From A Vector Of A Struct?

Apr 5, 2013

I'm trying to print values from a vector of a struct and I don't really know how to do that. Here is a simple code that I have for now to test how to add data to the vector then attempt to print it out.

#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <fstream>
using namespace std;
struct Employee//employee data

[Code]...

View 2 Replies View Related

C++ :: Accessing Pointed-to Value In A Struct Vector Of Pointers?

Apr 30, 2013

I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:

vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:

for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/
for (int b = 0; b < instance.structures[a].size(); b++){
for (int c = 0; c < instance.structures[a][b].size(); c++) {

if (1) { //checking value of variable not included in snippet

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);
}
}
}
}

The problem is in these two lines, I think:

(instance.structures)[a][b][c] = smgr->addAnimatedMeshSceneNode(fl);
(instance.structures)[a][b][c]->setPosition(renderPos);

These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?

View 4 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/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 :: Printing A Char String From A Struct

Jan 3, 2015

I am trying to save 5 persons names to a struct, and then printing them afterwards, shortly before the program ends. I tried to print the char string out right after it has been copied over, and it showed fine, but when i try to write it out right at the end of the program (its in a separate function) the terminal just prints gibberish.

the function looks like this:

Code:
int printUser(){
printf("Following patients have been recorded in this session:
");
struct database patient1;
struct database patient2;
struct database patient3;

[Code]...

the output looks like this(as you can se in under structest, that it shows the correct string, it also uses printf):

View 7 Replies View Related

C :: Splitting String And Copying It Over To A Struct

Sep 26, 2013

I'm having trouble with this code. What I'm trying to do is to read a line from a file and cut that line into two pieces, one is the keyword and the other is the definition. I want to read up to when there is a dash and assign that line to key and then assign the rest of the line to def. After that I copy key to the struct DictEntries.key and def to DictEntries.def. The output of this shows only the definition for both DictEntries.key and DictEntries.def but if I use "puts(key);" I see the keyword.

Code:

while(!feof(dictionary))
{
char line[200];
char *key,*def;
fgets(line,sizeof(line),dictionary);
key = strtok(line,"-");
}

[code]....

View 12 Replies View Related

C++ :: Find Frequency (string Into Struct Array)?

Apr 17, 2013

I'm trying to figure out the word frequency of a user inserted string. I've tried doing it via getline of the struct array and via the getline of the string word but neither either the former crashes or the latter just prints out the whole string.

Here is the latter.

#include "stdafx.h"
#include <iostream>
#include <string>
#define MAX_WORDS 200
using namespace std;

[Code] ....

How to handle the array of structs.

View 5 Replies View Related

C++ :: Assign String To Char Array Of Struct

May 6, 2013

#include <iostream>
using namespace std;
struct box{

[Code].....

C++Dev.cpp:23: error: incompatible types in assignment of ‘const char [15]’ to ‘char [40]’

View 2 Replies View Related

C++ :: Write Struct With String In Binary File

Jan 11, 2015

I have a structure :

struct Entrainement{
string muscleCible, nom, unitesObjectif;
double objectifActuel, dernierObjectif, objectifInitial, capaciteInitiale, progression[10];
};

and I want to write in a binary file the structure. The following works perfectly....

(*pointeurFichierEntrainementBin).write((char*)&exercice, sizeof(Entrainement));

except when the string exceed 11 characters. I guess it's because it has to pick a fixed sized for the string? but what if I want to always be able to have string up to 200 character? because now I can't exceed 11..I know writing a string with c_str() works, but I would like to write/read the structure in one shot.

View 1 Replies View Related

C/C++ :: String Copy Into Struct Doesn't Work

Nov 1, 2012

i'm right now using C, IO is done via ncurses, but that won't affect the following problem, i think. The relevant code is:

#define SIDEBARWIDTH 27
//...
typedef struct {

[Code]...

surprisingly this works, now the new 3rd outputline is correct again. So it seems that the printcommand has some troubles with accessing the struct here. Not sure if that might be ncurses fault. Still feels odd.

View 2 Replies View Related

C++ :: Initializing Const Struct When Data Is A String Literal

Feb 23, 2015

I have a struct like this:

Code:
struct String{
char* data;
std::size_t size;
};

I would like to be able to create const variables of this type for string literals.

Code:
const String message;

Is there an elegant way to create a const String like this when data is a string literal?

I tried this:

Code:
const char *string_data = "Hello";
size_t string_size = strlen(string_data) + 1;
const String string = {string_data, string_size};

The problem with that is that string.data isn't considered const during the initialization of the String struct so the compiler throws an error. It doesn't feel very elegant to do it like this either way.

Is there an elegant solution to this problem? I would like to avoid making a copy of the string literal.

View 7 Replies View Related

C :: Linear Search Function Accessing String In A Struct?

Apr 5, 2013

I currently have a file which allows inputs to record different transistor types. I then have the task of accessing this structure, find a certain manufacturer ID, and print the information about this particular transistor.

My problem is accessing the array to search through.

Here is my code:

Code:
#include "stdio.h"
const int IDLEN=30; //All constant values defined
const int POLARITYLEN=3;
const int MAXSTOCKITEMS=10;
//First structure defined
struct TransistorRec {

[Code]......

The errors I am currently getting are on line 54 'expected primary-expression before "struct"' and on line 60 ' 'maunfacturersID' undeclared'

View 11 Replies View Related

C++ :: Find String In Vector?

Oct 15, 2013

So the program reads contents from a text file into a vector and then the user enters in a string that they want to search for. The program iterates through the vector to find the string and then saves that line to another vector to display later(incase there is more then 1 instance of the string found).

Here is what I have atm:

void doSearch(vector<string> &phonelist, string searcher, vector<string> &holdNumbers)
{
int i = 0;
string value;

[Code].....

I just get an R6010 error -abort() has been called.

View 2 Replies View Related







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