Notices

Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

c++ string search

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 10-03-11, 03:13 PM Thread Starter   #1
ssjwizard
Member

 
ssjwizard's Avatar 

Join Date: Mar 2002
Location: Rio Rancho, NM

10 Year Badge
 
c++ string search


Hey guys its me again. Im in the middle of writing a class that can do a crapload of manipulation of arrays of strings. Ive got all of my code working great except for this one last tidbit.

I need to be able to take a user input string search the array and store any names found to be matching. The list is in the format of Last, First. For some reason I just cant get this bit to return any results ever. Ive tried casting the positions as int and that did not make a difference.


Code:
bool NameSearch::findFirstNames(vector <string> &vsFirst, string name){
	size_t found;
	bool find=false;
	for (int i=0;i<total;i++){
		found=names[i].find(name);
		if (found!=string::npos && found>names[i].find(",")){
			vsFirst.push_back(names[i]);
			find=true;
		}
	}
	return find;
}

bool NameSearch::findLastNames(vector<string> &vsLast, string name){
	size_t found;
	bool find=false;
	for (int i=0;i<total;i++){
		found=names[i].find(name);
		if (found!=string::npos && found<names[i].find(",")){
			vsLast.push_back(names[i]);
			find=true;
		}
	}
	return find;
}
Thanks for taking the time to look over this!

__________________
Life is what you make of it, we exist for the sole purpose of existing. Dont waste the universe efforts!
All statements, imagery, and ideas contained within any posts are merely retold tales of fictional acts by a figment of the universe imagination.

So your looking for an AMD FX motherboard, things you need to know.
Whats the max safe temps/volts for an FX?

Green Gorilla, CM 690-II nVidia, FX 8320, GTX 670, liquid cooled
Liquid Fusion, CM 690-II, A8+6670 Dual GFX, liquid cooled
Extreme evaporative water loop
450mm^3 water cooled beast - Incomplete
ssjwizard is offline Benching Profile Folding Profile Heatware Profile   QUOTE Thanks
Old 10-04-11, 07:47 PM   #2
I.M.O.G.
Glorious Leader

 
I.M.O.G.'s Avatar 

Join Date: Nov 2002
Location: Rootstown, OH

10 Year Badge
 
Free ...Get anywhere on your own yet? I'm sure theres other C++ guys around here

I don't know jack about C++ unfortunately, did a little bit of C# in my younger years, but its long since forgotten.

__________________
The OC Forums Way
We are a team. We are a community. We are a fellowship made strong by mutual respect and shared dedication to the task of enriching all who come here.
The OC Forums Thank You Thread
Put your computer to work for our OC Forum Teams!
Try out our POST TEMPLATES, they save you time answering common questions!

I spend half my money on CPUs, GPUs, and Liquid Nitrogen. The other half I waste.
I.M.O.G. is offline Author Profile Benching Profile Folding Profile Heatware Profile   QUOTE Thanks
Old 10-04-11, 09:54 PM   #3
Quigsby
Member

 
Quigsby's Avatar 

Join Date: Feb 2004
Location: Fargo, ND

 
In
Code:
for (int i=0;i<total;i++){
Where is "total" being set?

__________________
Quigsby
Rig 1: Gigabyte GA-P35-DS3L / Intel E8400 w/ Rosewill cooler / Antec 900 / 2GBx2 OCZ SLI PC800 / EVGA 9600GT / Cooler Master 750W
Rig 2: abit IP35-E / Intel E8500 w/ HDT-S1383 / Chenming case / 2GBx2 G.Skill 1066 / 60GB OCZ Agility + WD640 / EVGA 8800GS / Fortron 500W Blue Storm
Heatware
Quigsby is offline   QUOTE Thanks
Old 10-05-11, 12:18 AM   #4
pcarini
Member



Join Date: Nov 2008

 
I'm pretty sure that Quigsby is right on and 'total' isn't correct. Try replacing your for statements above with the following (This is assuming, of course, that 'names' is a vector<string> type class member.):
Code:
for(int i=0;i<names.size();i++)
It may not be as efficient as tracking the size yourself (it won't be much less efficient, though) but it is a lot more foolproof

This worked for me for testing.. bad code, but OK for a test:
Code:
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class NameSearch
{
	public:
		vector<string> names;

		void add(const string& name) { names.push_back(name); }

		bool findLastNames(vector<string> &vsLast, string name){
			size_t found;
			size_t total = names.size();
			bool find=false;
			for (int i=0;i<total;i++){
				found=names[i].find(name);
				if (found!=string::npos && found<names[i].find(",")){
					vsLast.push_back(names[i]);
					find=true;
				}
			}
			return find;
		}
};

int _tmain(int argc, _TCHAR* argv[])
{
	NameSearch ns;
	vector<string> found_last;

	ns.add("Test, Timmy");
	ns.add("Doe, John");
	ns.add("Doe, Jane");

	bool found = ns.findLastNames(found_last, "Test");
	if( found )
	{
		std::cout << "Name(s) found\n";
		for( size_t i=0; i<found_last.size(); i++)
			std::cout << found_last[i] << "\n";
	}
	else
		std::cout << "No name(s) found\n";

	std::cout << "Press any key\n";
	return 0;
}
/*
Name(s) found
Test, Timmy
*/

__________________
"We can't stop here, this is bat country!"
pcarini is offline   QUOTE Thanks
Old 10-05-11, 12:57 AM Thread Starter   #5
ssjwizard
Member

 
ssjwizard's Avatar 

Join Date: Mar 2002
Location: Rio Rancho, NM

10 Year Badge
 
total was set from the readfile method, names was a preexisting array I had to work with. I figured out my issue though. The data was in all caps so I just wrote a quick upcase function to ensure the user doesnt mess that up.

__________________
Life is what you make of it, we exist for the sole purpose of existing. Dont waste the universe efforts!
All statements, imagery, and ideas contained within any posts are merely retold tales of fictional acts by a figment of the universe imagination.

So your looking for an AMD FX motherboard, things you need to know.
Whats the max safe temps/volts for an FX?

Green Gorilla, CM 690-II nVidia, FX 8320, GTX 670, liquid cooled
Liquid Fusion, CM 690-II, A8+6670 Dual GFX, liquid cooled
Extreme evaporative water loop
450mm^3 water cooled beast - Incomplete
ssjwizard is offline Benching Profile Folding Profile Heatware Profile   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Programming Tips and Tricks
Programming Tips and Tricks
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 07:02 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?