Jump to content
  • Advertisement
Sign in to follow this  
metalman666

[C++] Template vector argument

This topic is 3226 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! everyone, I have 3 functions that reads some data from a string and stores it in a vector list. The 3 functions are for storing in a string vector, int vector and a float vector. I want to make these 3 functions as a general templatised version, but was unable to do it. I am getting compile errors. Can anyone suggest a way to do this? These are the 3 functions.
void ReadStringData( const string &readstring, std::vector<string> &_list );
void ReadFloatData( string &readstring, std::vector<float> &_list);
void ReadIntData( str &readstring, std::vector<uint32_t> &_list);
I have created a generic template function but can't seem to make it work.
template<typename T1,typename T2>
void ReadData(const T1& data, T2& list)
{
        int32_t lastPos = 0;
	for (size_t i=0; i<data.length(); ++i)
	{
		int32_t pos = data.find(delimiter_colon, 0, lastPos);
		
		if (0 == data.length())
		{
			break;
		}
		
		if (1 != pos)
		{
			string temp = data.substr(lastPos, pos-lastPos);
			list.push_back(temp);
			lastPos = pos+1;
		}
	}
}

How do i call this function with proper arguments? Any inputs will be very helpful. Cheers! Happy holidays.

Share this post


Link to post
Share on other sites
Advertisement
Well your function is actually not generic as you are pushing a string into the vector when the vector's contained type may not be string.
Seeing as you have the requirement that types are encoded in strings then there is no need for the first template type and it can be wrote like the following (untested code)


template<typename Type>
void ReadData(const std::string& data, std::vector<Type>& structure)
{
std::string::size_type start(0);
std::string::size_type finish(0);
std::string::size_type const total_size = data.size();

while(start < total_size)
{
start = data.find_first_not_of(":",start);
if(start == std::string::npos)return;
finish = data.find_first_of(":",start);
if(finish == std::string::npos) finish = total_size;
std::string value = data.substr(start,finish-start);

std::stringstream ss(value);
Type typedValue;
ss >> typedValue;
structure.push_back(typedValue);
start = finish;
}
}



You would then call the function like


int main()
{
std::vector<float> f;
std::string floats ("1.1:2.2:3.3"):
readData(floats,f);
}

Share this post


Link to post
Share on other sites
A code that compiles:


template< class T, template< class, class > class Container >
void ReadData(
std::string const & data,
Container< T, std::allocator< T > > & list
)
{
int lastPos = 0;

for ( size_t i = 0; i < data.length(); ++i )
{
int pos = data.find( ";", 0, lastPos );

if ( 0 == data.length() )
{
break;
}

if ( 1 != pos )
{
std::string temp = data.substr(lastPos, pos-lastPos);
list.push_back(temp);
lastPos = pos+1;
}
}
}








Actually, what you want to achieve is impossible...You cannot store a string instance in a vector of floats. Your code will only compile for a container of string, that has a member function template< typename T, template< typename > class allocator > void Container< T, allocator< T > >::push_back( T const & ).

Hence,


template< template< class, class > class Container >
void ReadData(
std::string const & data,
Container< std::string, std::allocator< std::string > > & list
)
{
int lastPos = 0;

for ( size_t i = 0; i < data.length(); ++i )
{
int pos = data.find( ";", 0, lastPos );

if ( 0 == data.length() )
{
break;
}

if ( 1 != pos )
{
std::string temp = data.substr(lastPos, pos-lastPos);
list.push_back(temp);
lastPos = pos+1;
}
}
}






Share this post


Link to post
Share on other sites
Quote:
Original post by johnstanp
A code that compiles:

*** Source Snippet Removed ***

Actually, what you want to achieve is impossible...You cannot store a string instance in a vector of floats. Your code will only compile for a container of string, that has a member function template< typename T, template< typename > class allocator > void Container< T, allocator< T > >::push_back( T const & ).

Hence,

*** Source Snippet Removed ***


I understand that, actually that implementation was for the strings vector version and i forgot to change it in the template version.

Thanks a lot to both of you for your suggestions. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!