Search sub-string in vector and write whole string to file

Hello,

I'm having a problem with finding and printing an element in a vector.

I want to search my vector to see if an element starts with a sub-string, and if it does, I want to write the whole string to a file.
The first part is done, I just can't figure out how to write the whole string to file.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ifstream file1(filename);
std::vector<std::string> load;
for(std::string line1; getline(file1, line1); )
	{
		load.push_back(line1);
	}

	for(int i = 1; i < 1018; ++i)
	{
		std::ostringstream integer;
		integer << "IDS_STRING" << i << "\t";
		std::string thisString = integer.str();
		bool isPresent = std::find_if(load.begin(), load.end(), StartsWith(thisString)) != load.end();
		if(isPresent == true)
		{
			RC << ??? + "\n";
		}
	}
}


I've put '???' where I'm having trouble, which is what to put for the output so that the entire element (not just the first part I searched for) is written to the file (RC).

Any ideas/suggestions would be much appreciated.

Sorry, I forgot to add this part of the code:

1
2
3
4
5
6
7
struct StartsWith {
    const std::string val;
    StartsWith(const std::string& s) : val(s) {}
    bool operator()(const std::string& in) const
    {
        return in.find(val) == 0;
    }
Last edited on
Is this what you want? RC << thisString << "\n";
Thanks for the quick reply, but no, that's not what I want. I originally tried that, but it only writes the part of the element searched for, whereas the elements in the vector actually contain longer strings, e.g. IDS_STRING5 "Actuator".

So I need to search for the first part (IDS_STRING5) but write the whole thing to file (IDS_STRING5 "Actuator").
I've got it figured out, needed an iterator.

Code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(int i = 1; i < 1018; ++i)
{
	std::ostringstream integer;
	integer << "IDS_STRING" << i << "\t";
	std::string thisString = integer.str();
	bool isPresent = std::find_if(load.begin(), load.end(), StartsWith(thisString)) != load.end();
	if(isPresent == true)
	{
		std::vector<std::string>::iterator vit = std::find_if(load.begin(), load.end(), StartsWith(thisString));
		if (vit != load.end ()) 
		{
			RC << *vit + "\n";
		}
	}
}
Topic archived. No new replies allowed.