Page 1 of 1

It's official: I code better at night.

Posted: Mon Dec 08, 2008 2:03 am
by Red Squirrel
I just wrote a string vector manipulation function where I pass a vector of strings and then it returns a new vector with only the strings where the start/end match what I specify, with option of specifying if they should be truncated.

So say I pass this list:

test.Add("start_data1_end");
test.Add("more data1");
test.Add("start_this2");
test.Add("data3");
test.Add("tstart_hat4");
test.Add("data5");
test.Add("start_this3_end");


I specify start_ and _end as the parameters the end result should be:

start_data1_end
start_this3_end


If I choose to truncate then it's this:

data1
this3


I compiled, it works first shot. O_o

I'm really loving this late night shift, enabling me to go to bed late.



For those curious this is the function:

Code: Select all

	xVector<string> Parser::FilterIn(xVector<string> data,string start,string end,bool truncate)
	{
		xVector<string> ret;
		
		string tmp;
		bool markasgood;
		
		for(int i=0;data.Get(tmp,i);i++)
		{
			markasgood=false;
			
			if((start.size()<=0 || tmp.find(start,0)==0) && (end.size()<=0 || tmp.find(end,tmp.size()-end.size())!=string::npos))
			{
				markasgood=true;
				
				if(truncate)
				{
					tmp = DeleteOne(tmp,start,0,1);
					tmp = DeleteOne(tmp,end,tmp.size()-end.size(),tmp.size());			
				}			
			}
			
			if(markasgood)ret.Add(tmp);		
		}
		return ret;
	}
Archived topic from AOV, old topic ID:3923, old post ID:25243

It's official: I code better at night.

Posted: Mon Dec 08, 2008 11:21 am
by dprantl
I love vectors. They are the best data storage type ever! Even better than hashtables in most cases.

Archived topic from AOV, old topic ID:3923, old post ID:25247

It's official: I code better at night.

Posted: Mon Dec 08, 2008 12:19 pm
by Death
dprantl wrote:I love vectors. They are the best data storage type ever! Even better than hashtables in most cases.
Oh God....hashtables. Certainly brings back some bitter memories.

Archived topic from AOV, old topic ID:3923, old post ID:25251

It's official: I code better at night.

Posted: Mon Dec 08, 2008 3:44 pm
by Red Squirrel
They can be rather useful but what I normally end up doing is just put a custom type inside a vector instead, sometimes even make a container class to store it in.

Archived topic from AOV, old topic ID:3923, old post ID:25258