C++ :: Search A Vector Element With Multiple Strings?

Nov 22, 2013

How could I search a vector element with multiple strings

i.e. vector.at(i) = This is a test

How could I search that to look for the word "test" in that element?

View 7 Replies


ADVERTISEMENT

Visual C++ :: How To Do Binary Search On A Vector Of Strings

Sep 25, 2012

I'm trying to do a binary search on a vector of strings and keep getting this error. This is the PhoneEntry header file with the class declaration:

Code:
using namespace std;
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#include"phoneNumber.h"

[Code] .....

View 5 Replies View Related

C++ :: Search For Element By Name - Printing Linked List

May 2, 2014

I've got this program that I'm working on. Most of the code is from a video tutorial, but I was editing it to be able to search for an element by name. That's working fine, but suddenly the part of the program that prints out all the elements starts in an infinite loop after I input two elements, and search for one.

Here's what I've got:

[code#include <iostream> usingnamespacestd; struct node { int number; string name; node *next; }; bool isEmpty (node *head); char menu (); void insertAsFirstElement (node *&head, node *&last, int number); void insertSorted(node *&head, node *&last, int number); void remove(node *&head, node *&last); void showList (node *current); void showByName (node *current, string name); bool isEmpty (node *head) { if (head == NULL) { return true; } else { returnfalse; } } char menu () { char choice;

[Code] .....

View 8 Replies View Related

C++ :: Binary Search Function - Return True If Element Inputted By User Found In Array And False If Not

Nov 9, 2014

I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//binary search function
bool search (int array[], int item)

[Code] ....

Why my code does not fulfill it's purpose???

View 7 Replies View Related

C :: Function To Search If Two Given Strings Are Equal

Jul 9, 2014

I made my own function to search if two given strings in my function are equal but the problem is if i pass two variable like hello,hello ... result is string equal but if i pass hello , hello also give me string equal because last 4 characters same to last 4 characters of hello ...

Code:
int getSimilarityOfTwoStrings(const char str1[],const char str2[]){
int str1Len = getStringLength(str1);
int str2Len = getStringLength(str2);
int i = 0;
int j = 0;
bool truefalse;

[Code] .....

View 3 Replies View Related

C++ :: How To Insert Element Into A Vector At Particular Index

May 3, 2013

I am storing info in a vector but I want to store certain info at a particular index. I am new to using vectors and am unsure about have to do this. I am aware of the insert method but am confused on how to use it to store at a particular index.

View 1 Replies View Related

C++ :: Pop Element On A Vector Off And Return Its Value At Same Time

Sep 6, 2014

I am trying to pop the element on a vector off and return its value at the same time

vector.push_back();

Unfortunately that code only removes that element from the vector it does not return it

Is the only way to get the element and destroy it is to do this?

vector.back();
vector.pop_back();

View 1 Replies View Related

C++ :: How To Access Element Of Point3f Vector

Aug 4, 2013

How to access an element of the point3f vector. E.g. :

int main() {
vector<Point3f> a;
a.push_back(Point3f(0,0,0);
a.push_back(Point3f(0,0,1);

//print first x,y,z element
cout<<a[0]<<endl;

[Code] ...

But it doesn't work firstly because it says can't use typename i.e. <float>

View 8 Replies View Related

C++ :: How To Remove Element Of A Vector And Returning It

Sep 22, 2014

pop_back just returns void, so I just can't use that?

erase is okay but it doesn't return anything....

Do I use a combination of both?

View 2 Replies View Related

C++ :: Search Array Of Strings - How Many Time Word Was Used

Feb 4, 2013

The function should search the array of strings to find how many times the word 'the' was used. but its only returning a 0.

int FindThe (int The, char Paragraph[], char Pattern[]) {
for (int i(0); i < 500; i++) {
if (Paragraph[i] == Pattern[0] {

[Code] ....

View 5 Replies View Related

C/C++ :: Search In File And Extract Content Between 2 Strings

Jun 8, 2014

I need to develop a simple program, i have 2 variables (begin, end), and i need to search in a file, And extract the string between the Begin and the End variables to a new File, For Example:

my text file: file.txt:

some text here<StartHere>more text here</EndHere>text text

C++ Program:

//Declear 2 variables
strcpy_s(begin, string("<StartHere>").c_str());
strcpy_s(end, string("</EndHere>").c_str());

//And now, search in the Text file, And Extract the text between the begin string and the End string.
<...>

The Result should be: NewFile.txt with the content:

<StartHere>more text here</EndHere>

That's it!, Here is what i have for now:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
int ocurrences_count = 0;
int ocurrences2_count = 0;
char word[20]; //this array will save user input

[Code] ....

View 1 Replies View Related

C++ :: Using Istringstream To Read Each Word Of Each Element In Vector

Mar 6, 2014

Consider I have a vector of strings and then I use an istringstream to read each word of each element in the vector, why do I nescessarily use an istringstream?

This is the code that does what I just described (I think)..

Code:
for(auto &elem : svec) {
istringstream strm(elem);
while (strm >> word)
//Magic
}

What would be the equivalent of using something else than an istringstream in this scenario and how does the istringstream work?

View 7 Replies View Related

C++ :: Changing Class Type Of A Vector Element?

Aug 16, 2014

I am programming a 2-D platformer video game. The stages are composed of an array (really a vector) of 16x16 px^2 tiles. I have defined a base class "Tile" and several derived classes, e.g., "Ramp", "Door", etc., which have their own attributes. The idea is that upon entering a room, the program will load all of the necessary tile data for that room into a vector. So, I have a vector that looks like: vector <Tile*> room_tiles, and resize it based on the total number of tiles in the room: room_tiles.resize(Tile_Count). I then want to read in certain info from the data file containing all of the tile information for that room. For example, if the data file says Tile 5 should be a ramp, I want to change the 5th element of the room_tiles vector to the derived ramp class. This is really where I'm having trouble. I've worked with vectors of base and derived classes before, but those were always of indeterminate size and I always used something like: (Vector).push_back(new DerivedClass()) to specify the derived class of that element. The problem is that that method only seems to work if you are appending elements to the end of a vector.

how I can do this?

View 7 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 Vector From A File - Adding Empty Element At The End

Apr 20, 2014

I'm having a problem filling a vector from a file. Basically, it is adding an empty element at the end. I'm new to Qt and haven't worked with file streams much so how to stop the stream before it adds the extra element.

void gui::get_data() {
mileage.clear();
QFile file(file_label->text() + ".txt");
QTextStream in(& file);
float m;
float g;
QString d;

[Code] ....

But, if I add another element to the vector and write that the file look like this.

//file after adding element
132654 0 02132014
132654 0 02132014
0 0
132998 22 02202014

I have it set to append at the moment so that is why the first line is repeated. I figure the problem is with if(in.atEnd()). I could fix it by deleting the last element right after adding it, but that seems like more of a hack than anything else.

View 3 Replies View Related

C++ :: Sequentially Remove One Element From Vector At A Time With Replacement?

Dec 8, 2014

I have a vector of int,

Code:
vector<int> row_numbers{1,2,3,4,5,6,7,8,9};

I want to sequentially remove one element at a time starting with the first. When the second element is removed, the first element needs to go back in. The sequence would look like

Code:
// original vector, row_numbers.size()=9
row_numbers{1,2,3,4,5,6,7,8,9};
// trimmed vector, row_numbers_trim.size()=8

[Code] .......

I have been working under the assumption that the best method would be to have row_numbers remain untouched and work on a copy. For each step in the sequence, you would create row_numbers_trim as a copy of row_numbers, and then remove an element from row_numbers_trim.

Code:
// position being removed
int counter = 0;
// copy original vector
row_numbers_trim = row_numbers;
// remove the first element from the copy
row_numbers_trim(row_numbers_trim.begin()+counter);

All you would have to do here is to increment counter in a loop. is there a better way?

View 8 Replies View Related

Visual C++ :: Selecting 1 Element From Each Vector Without Duplication Of Any Combination

Jul 15, 2013

I have N vectors which look like this:

[1→m] [m+1→2m] [2m+1→3m] [3m+1→4m] [4m+1→5m]..... [{(N-1)m}+1→Nm]

I want to select 1 element from each vector without duplication of any combinations. Essentially only when all combinations are done with 1st element in first vector ,only then it should move to next element in first vector.

Say i have elements :[123] [456] [789]

my combinations should be like
147
148
149
157
158
159
167
168
169
247....

Also, I cant have any repetitions and only after all combinations of 1 are done only then the loop has to move to next combination ie 247 combination and so on.

I tried NCK (n choose k) command but it gave me random combinations. How should i go about it with using minimal for loops?

View 2 Replies View Related

C++ :: Decipher Multiple Strings From TXT File

Nov 17, 2013

I need making a function that is deciphering multiple strings from a .txt file. The function is supposed to remove all instances of the first three characters from the string.

An example would be from l.pjkjsdfl.p[)sdfslkl.p ------> jkjsdf[)sdfslk

Basically it removes "l.p" from wherever it is in the string.

I am new to C++ and I don't know where to start from.

View 1 Replies View Related

C++ :: Converting Multiple Lines Of Strings To Double

Aug 26, 2014

I am trying to convert multiple lines of strings to double with std::stod .... I used the code found on the website:

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
int main() {
std::string two_doubles =" 22145.365 -3564789";
std::string::size_type K;
double first = std::stod (two_doubles, &K);
double second = std::stod (two_doubles.substr(K));
}

The string starts with white spaces. I get this error message when compiling:

warning unused variable 'first'
warning unused variable 'second'

How do you convert the two numbers in the string two_doubles to doubles?

View 2 Replies View Related

C++ :: Enter Multiple Strings Into Char Array

Feb 16, 2014

I am having issues getting this working. This is a simple program that is designed to ask a user if he would like to enter a string, if yes, the user is prompted to enter it and its stored in a char array. User is then asked if he wants to enter another string... Once user responds no, the program outputs the strings and the program ends...

Note: I'm using in.getline(myarray[i], MAX, ' '), to avoid white space problems if user enters a space. Lastly I would like the option of letting user enter any number of strings, but how would you do this when declaring the 2 dimensional char array?

#include <iostream>
#include <string>
using namespace std;
const int MAX = 81; //max char is sting is 80
int main(){
string y_n;
bool go = true;

[Code] ....

View 2 Replies View Related

C++ :: How To Search The Loop Of Queues Of Vector

Feb 14, 2013

i have a paradigm where a integer before gets enqueued to a queue, the loop of queues in a vector is searched and integer is enqueued to a queue which has minimum size among the queues. the following code shows the operation.

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
if(q[min_index].size() > q[i].size())
min_index = i; // Now q[min_index] is the shortest queue}
q[min_index].push(int)

next i am trying to extend my paradigm with that the condition the integers should be enqueued to the shortest queue until it becomes maximum size among the queues.

do{
q[min_index].push(int)
} while(q[min_index].size > queue sizes in the vector loop except this queue )

how to search the loop of queues of vector in the while ()

View 13 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++ :: Strings - Read Multiple Values In On A Single Line

Jul 28, 2014

My question is on c++ strings. At the moment my program is reading input in one line at a time after the user presses enter.

I want to read multiple values in on a single line. Example: "apple banana orange end" ... How would I do this?

MAIN Code:
#include "Header.h"
#include "Orange.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

[Code] ......

View 11 Replies View Related

Visual C++ :: Finding Items (strings) In Multiple Lists

Mar 12, 2013

Im working on a script compiler and i need to handle different types of data. Actually different categories of items.

Let's say i have two categories: cat's and bird's. They are different and stored in different lists. And let's say there is a simple command: GIVE_FOOD_TO(animal_type, food_type) Animal type here can be either from birds category or cat's category.

And also let's say user gives command: GIVE_FOOD_TO(cat1, fish)
and also for example: GIVE_FOOD_TO(bird1, birdfood)

Variable names could be anything, but im storing each variable name in std::map so later i can figure out with what animal current command is used.

When im parsing the script then i must know if user supplied either cat or bird. If i just would have cat's or bird's category then i would have only 2 lists and not a big problem to loop through either cats list or birds list and find out in what list the "cat1" is or in what particular list the "bird1" is. It would be in one or another.

But i have a lot of categories and looping through all of these lists (or std::maps) is slow and doesn't seem like a good. Just to find out in what list it's stored. I can't rely on variable names, they could be anything.

Big picture atm:

1) I have one BIG box which stores all of the categories

2) When i need to find out to which category the variable (animal_type) point's to i must get it quickly, dunno, std::map in std::map or something?

What i need basically is: I have different lists (each one is just a category for either birds or cats in this example) And when i have variable name, i must find out quickly in what particular category this item is stored. So i can work with it.

View 4 Replies View Related

C++ :: Search In A Vector Array From User Input String?

Sep 30, 2013

How would you search in a vector array from a user input string?

ex: user input : "Hello"

output: search vector array and find the line that has the string "Hello" and output the array "Hello" is on?

View 1 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