读书人

关于字符串的小疑点

发布时间: 2013-03-14 10:33:15 作者: rapoo

关于字符串的小问题
假如给一个字符串"abc,def,ghi,hij"
然后用以','字符进行分割,然后储存在数组中就是
a[0] = "abc" ;
a[1] = "def" ;
a[2] = "ghi" ;
a[3] = "hij" ;
可以使用string或者CStirng,或者直接使用c语言的函数,帮我看看。
[解决办法]


#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef vector< string > vec_str;
typedef vector< string >::iterator vec_str_iter;

vec_str split(const string& str);

int main()
{
string str("abc,def,ghi,hij");
vec_str vs = split(str);
for (vec_str_iter i = vs.begin(); i != vs.end(); ++i)
{
cout << *i << endl;
}
}

vec_str split(const string& str)
{
vec_str ret;
string::const_iterator first = str.begin();
for (string::const_iterator i = str.begin(); i != str.end(); ++i)
{
if (*i == ',')
{
if (*first != ',')
{
ret.push_back(string(first, i));
}
first = i+1;
}
}

if (first != str.end())
{
ret.push_back(string(first, str.end()));
}

return ret;
}

[解决办法]
#include "stdafx.h"
#include <iostream>

// cSplit是分割字符,调用时传进你需要的','即可
void splitSTr(const char * srcStr ,char cSplit)
{
const char *p1 = srcStr;
const char *p2 = srcStr;
string str (srcStr);
while(*p2 != '\0')
{
if (*p2 == cSplit)
{
cout << "[" <<str.substr((p1-srcStr) ,(p2-p1)) << "]" << endl; // 这里就是分解的子字符串,可以放进一个vector<string>里
p2++;
p1=p2;
}
else
{
p2++;


}
}
cout << "[" <<str.substr((p1-srcStr) ,(p2-p1)) << "]" << endl; // 别忘了最后一个,同样处理
}

int _tmain(int argc, _TCHAR* argv[])
{

char *str = "abc,,de,fg,hhjy,kjd,";
splitSTr(str ,',');
return 0;
}


[解决办法]
[root@down cpp]# ./main 
abc
def
ghi
hij
[root@down cpp]# cat main.cpp
#include <iostream>
#include <string>
#include <sstream>

int main(int argc, char *const argv[])
{
std::string input = "abc,def,ghi,hij";

std::istringstream istrm(input);
std::string word;
while (getline(istrm, word, ',')) {
std::cout << word << std::endl;
}

return 0;
}


sstream效率是不错的.
[解决办法]
引用:
C/C++ code?12345678910111213141516171819202122[root@down cpp]# ./main abcdefghihij[root@down cpp]# cat main.cpp #include <iostream>#include <string>#include <sstream> int main(int argc, c……


better:-)
[解决办法]
strtok, wcstok, _mbstok
Find the next token in a string.

char *strtok( char *strToken, const char *strDelimit );

wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );

unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );

Routine Required Header Compatibility
strtok <string.h> ANSI, Win 95, Win NT
wcstok <string.h> or <wchar.h> ANSI, Win 95, Win NT
_mbstok <mbstring.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.

Parameters

strToken

String containing token(s)

strDelimit



Set of delimiter characters

Remarks

The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcstok strtok _mbstok wcstok


On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.

Warning Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable effects.

Example

/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );


/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}


Output

A string of ,,tokens
and some more tokens

Tokens:
A
string
of
tokens
and
some
more
tokens


String Manipulation Routines

See Also strcspn, strspn, setlocale

读书人网 >C++

热点推荐