Skip to main content
GameDev.net gamedev.net
🔒 Locked

std string quick help

Started by Stashi Nov 27, 2001 at 5:55 PM 5 replies 700+ views
Original Post
Stashi
Stashi
sorry ive posted like 3 topics already :[ ok im using std strings, now im trying to run through the string and count every time a space appears in the string.. ive tried this..
  
for (int x=0; x<strInput.length(); x++) {
   if (strInput[x] == " ") 
   {
       spaceCount++;
   }
}
  
however i get the errors: C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(20) : error C2446: ''=='' : no conversion from ''char *'' to ''int'' This conversion requires a reinterpret_cast, a C-style cast or function-style cast C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(20) : error C2040: ''=='' : ''int'' differs in levels of indirection from ''char [2]'' Error executing cl.exe. i tried placing the space in single quotes but that generates more errors. how can i go about comparing individual elements of the string array?
Stashi
Stashi
oh also, how come i cannot do this..

  
#include <string>
#include <vector>

std::vector;
std::string;

vector <string> strArray;


this gives me a whoole bunch of errors:

C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(8) : error C2143: syntax error : missing '';'' before ''<''
C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(8) : error C2501: ''vector'' : missing storage-class or type specifiers
C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(8) : error C2143: syntax error : missing '';'' before ''<''
C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(9) : error C2146: syntax error : missing '';'' before identifier ''strInput''
C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(9) : error C2501: ''string'' : missing storage-class or type specifiers
C:\Microsoft Visual Studio\MyProjects\strs\main.cpp(9) : fatal error C1004: unexpected end of file found

why wont that work
Stashi
Stashi
quote:
i tried placing the space in single quotes but that generates more errors.


actually it generates 4 large warnings, such as:

c:\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::reverse_iterator,std::allocator > const *,std::basic_string,std::allocator >,std::basic_s
tring,std::allocator > const &,std::basic_string,std::allocator > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\microsoft visual studio\vc98\include\vector(39) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocatortraits,std::allocator > > >::std::vector,std::allocator >,std::allocator,std::allocator > > >(const std::allocatorchar,std::char_traits,std::allocator > > &)'





Edited by - Stashi on November 27, 2001 7:15:23 PM
Oluseyi
Oluseyi
There''s nothing you can do about the warnings; they''re just MSVC telling you that the expansion of the template type string is more than 255 characters in length, and can not be represented in the debugger. You can turn it off with a pragma.

As for finding all occurrences of a character/set of characters in a string, use find_first_of():

#pragma warning( disable : 4786 )
#include <string>
using std::string;
//
...
string str;
int pos0 = 0, pos1 = 0;
int nSpaces = 0;
while(true)
{
// find first occurence of space starting at pos0
pos1 = str.find_first_of(" ", pos0);
// break if no more found
if(pos1 == string::npos)
break;
// otherwise increment count
++nSpaces;
pos0 = pos1;
}



Start Here!
Oluseyi
Oluseyi
Oh yeah, you can also do trivial rejection (skip the entire test if there are no spaces in the string) using find_last_of().


Start Here!
Stashi
Stashi
thank you!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.