Hi all,

I've been trying to find part of a string in a vector. The problem being that I can only get it to work if I enter the entire string, but I need it so that I only search for the first part of the string in the vector.

Here's what I've got:

int i =1;
while (i < 4000)
{
	std::fstream file1("Chinese.txt");
	std::vector<std::string> load;
	std::stringstream integer;
	integer<<i;
	std::string thisString;
	thisString=integer.str();
	thisString.insert(0,"IDS_STRING");
	while(!file1.eof())
	{
		std::string line1;
		std::getline(file1,line1);
		load.push_back(line1);
	}
      bool isPresent=std::find(load.begin(),load.end(),thisString)!=load.end());
	if(isPresent==false)
	{
		file1<<thisString + "\t";
	}
	else {}
	file1.close();
	i++;
}

So what's happening here is that the file contains strings that begin with "IDS_STRING" followed by a number, a tab and then a command. I'm basically trying to go through the file and see if each IDS_STRING number exists, so from 1 to 4000, and if it doesn't I want to write a new line in the file with the IDS_STRING number.

Example file contents:

IDS_STRING3 "Exhaust in"
IDS_STRING10 "Actuator off"
IDS_STRING4 "Service"

And say, I search for IDS_STRING12, which doesn't exist, I'd want that line to be inserted into the file, so:

IDS_STRING3 "Exhaust in"
IDS_STRING10 "Actuator off"
IDS_STRING4 "Service"
IDS_STRING12

Now, I don't know what the second part of the string is going to be (the command) so can only search for the IDS_STRING part. How can I achieve that?

Thanks in advance

Instead of storing the entire line as a string, why not map the two values to key/value entries into a std::map ? Something along the lines of

while (file1.good ()) {
   file1 >> ids;
   file1.getline (val, VAL_SIZE);
   lut[key] = val;
}
// Then to look up a certain key...
if (lut.find (ids_key) != lut.end ()) {
   // you have a matching key...
}

so can only search for the IDS_STRING part. How can I achieve that?

try a little somethin' like this:

vector<string> text_file;

//load the vector........

//find substrings in your vector:
for(int i=0, size=text_file.size(); i<size; i++)
{
     if(text_file[i].substr("IDS_STRING") != string::npos)
     { 
          //found
     }
     else
     {
          //not found
     }
}

I'm basically trying to go through the file and see if each IDS_STRING number exists, so from 1 to 4000, and if it doesn't I want to write a new line in the file with the IDS_STRING number.

This isn't too terribly difficult; however, the data structure ye' have chosen (vector) is a poor container when it comes to insertion that does not occur at the very end. A better choice for your application would be the <list> class... which is very similar to <vector> but offers an efficient insert() function (<list> offers the efficiency of a double-linked list data structure, unlike the array type of <vector>) So, what you would want to do is to populate your list and sort it using the sort() function from <algorithm>. You would want to (and have to) use the overloaded version of sort() that allows you to pass in your own custom compare function. This will allow you the flexibility to parse the list elements and perform a strict < less than comparison between elements. Sort the list, perform insertions as desired, open a file to write to. (if file already exists, open using the ios::trunc flag to overwrite the file)

Edited 4 Years Ago by Clinton Portis: n/a

This question has already been answered. Start a new discussion instead.