1
A
답변
-1
알고리즘을 검색하려면 또는 토큰 크기 문자열을 검색하십시오. 사소한 일이야.
또한 문서를 확인하고 사용 가능한 도구를 사용할 수 있습니다 http://www.cplusplus.com/reference/string/string/
간단한 구현 될 수있다 :
void tokenize(const string & text, vector<string> & tokens, char delim)
{
size_t length = text.size();
string token = "";
for(size_t i=0;i<length;i++)
{
if(text[i] != delim)
{
token += text[i];
}
else
{
if(token.size() > 0)
{
tokens.push_back(token);
}
token = "";
}
}
tokens.push_back(token);
}
2
C++가 정확히이 일을 위해 기능이 내장되어 있지 않습니다. 그러나 std::string::find_first_of
멤버 함수 또는 비 멤버 std::find
을 사용하여 구현할 수 있습니다. 물론이 국경 많은 경우는 처리 할 수 있으며,이 구현은 정확하게 당신이 원하는 일을하지 않을 수도 있지만, 그것은 당신에게 출발점을 제공한다
#include <string>
#include <vector>
#include <algorithm>
// given a string str, split it on every occurrence of the character delim
std::vector<std::string> tokenize(std::string str, char delim) {
// store the results in a vector of strings
std::vector<std::string> tokens;
std::string::iterator end = str.end();
std::string::iterator left = str.begin();
for (;;) {
// find the next occurrence of the delimiter
std::string::iterator right = std::find(left, end, delim);
// create a string from the end of last one up until the one we just foun
tokens.push_back(std::string(left, right));
// if we reached the end of the string, exit the loop
if (right == end) { break; }
// otherwise, start the next iteration just past the delimiter we just found
left = right + 1;
}
return tokens;
}
// test program
int main() {
std::string str = "foo, bar, baz";
std::string str2 = "foo, bar, baz,";
std::string str3 = "foo";
std::string str4 = "";
std::string str5 = ",";
std::vector<std::string> tokens = tokenize(str, ',');
std::vector<std::string> tokens2 = tokenize(str2, ',');
std::vector<std::string> tokens3 = tokenize(str3, ',');
std::vector<std::string> tokens4 = tokenize(str4, ',');
std::vector<std::string> tokens5 = tokenize(str5, ',');
}
:
다음은 후자를 사용하는 예제입니다.
0
이렇게하는 또 다른 방법은 strtok
을 사용하는 것입니다. 이것은 오래된 c 방법이지만 여전히 문제에 적용됩니다.
using <vector>
using <string>
char* token, line[512];
std::string tokenStr;
std::string lineStr = "0, 1, 2";
std::vector<std::string> commaSplit;
strcpy (line, lineStr.c_str());
//Remove spaces and find the first instance of ','
token = strtok(line, " ,");
while(token != NULL)
{
//Copy the token to a string
tokenStr = token;
//Add the token to the vector
commaSplit.push_back(token);
//Find next instance of the ,
token = strtok(NULL, " ,");
}
관련 문제
- 1. 목표 - C 하위 문자열
- 2. C# Long 하위 문자열 함수
- 3. 하위 문자열 검색 문자열
- 4. 문자열 내의 하위 문자열 찾기
- 5. 자동으로 C 의존성 발견
- 6. BinarySearch - 문자열 요소를 발견 된 다른 문자열 요소로 바꾸기
- 7. 하위 문자열 2005
- 8. VB6 하위 문자열 색인
- 9. 하위 문자열의 문자열 테스트?
- 10. PHP 하위 문자열 정규식
- 11. Twitter 하위 문자열 검색
- 12. 문자열의 하위 문자열 범위
- 13. 자바 하위 문자열?
- 14. 하위 문자열 크기 조언
- 15. 하위 문자열 찾기
- 16. 어셈블리에서 하위 문자열 찾기
- 17. Jquery 하위 문자열 바꾸기
- 18. PHP - 문자열의 하위 문자열
- 19. 디스크 하위 문자열 색인
- 20. dijit.form.FilteringSelect 하위 문자열 검색
- 21. 하위 문자열 찾기
- 22. 문자열의 하위 문자열 바꾸기
- 23. MySql 필드 하위 문자열
- 24. Mips의 하위 문자열
- 25. Java에서 하위 문자열 검색
- 26. 하위 쿼리에서 문자열 반환
- 27. 두 문자열 사이의 모든 하위 문자열 찾기
- 28. tsql 하위 문자열 또는 문자열 조작
- 29. sql - 전체 문자열 대신 하위 문자열 비교
- 30. 문자열 배열에서 하위 문자열 검색 중?
http://stackoverflow.com/questions/599989/is-there-a-built-in-way-to-split-strings-in-c는 더 나은 복제본입니다. 첫 번째 질문은 공백에만 분할하는 방법을 구체적으로 묻습니다. – jalf