String Tokenization

Started by
2 comments, last by henrym 21 years, 7 months ago
I'm making a Console and I'm having lots of problems with my TokenizeCommand method:
      
bool CConsole::TokenizeCommand(const string &command)
{
	argc = 0;
	argv.clear();

	istringstream data(command);
	string token;

	//copy tokens to argv

	while (data >> token)
	{
		argv[argc++] = token;
	}

	return true;
}
  
argc is int, argv is std::vector<string>. It creates an unhandled exception : access violation. Code compiles fine, no errors but, a few warnings...which I have chosen to ignore (happens with all my projects using vector or string):
        
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> 
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
        c:\program files\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(const std::allocator<std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > > &)'
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
        c:\program files\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(const std::allocator<std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > > &)'
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
 >::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
 >::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
      
Is there anything at all wrong with my code? Henrym My Site [edited by - henrym on September 15, 2002 6:46:00 AM]
Advertisement
argv.push_back(token);
argc++;

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

VC++ Specific - Get RID of the warnings with

#pragma warning(disable:4786) // stl names truncated to 255 chars


Make sure you do this *before including anything of the STL!*
Otherwise these will still plop up!

Regards
/Roger

visit my website at www.kalmiya.com
Davepermen had the right answer. The explanation is that a vector is not an infinitely-sized array, and therefore you can''t just use the [] operator to add elements randomly. It needs to be of the appropriate size first. Using push_back handles this for you, as it increases the size by one and adds the element into that last position.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

This topic is closed to new replies.

Advertisement