C++ C+中的字符串操作+;使用strtok
我正在尝试从.gz文件中执行一些字符串操作。 我已经编写了以下代码C++ C+中的字符串操作+;使用strtok,c++,C++,我正在尝试从.gz文件中执行一些字符串操作。 我已经编写了以下代码 char buffer[1001]; for(;gzeof(f_Handle);){ gzread(f_Handle, buffer, 1000); buffer[1000] = 0; char* chars_array = strtok(buffer, " "); while(chars_array){ cout<<chars_array << '\n'
char buffer[1001];
for(;gzeof(f_Handle);){
gzread(f_Handle, buffer, 1000);
buffer[1000] = 0;
char* chars_array = strtok(buffer, " ");
while(chars_array){
cout<<chars_array << '\n';
chars_array = strtok(NULL, " ");
}
}
我想知道什么时候是A或B,以及A或B中的内容
目前,它以以下方式打印出来
A
1
2
3
...
想法a)是使用if循环通过chars_数组找出a或B或
b) 字符串数组而不是字符指针这里是一个使用
std::string
和函数substr(…)
的简单示例,它不会处理整个字符串,但您可以将它放在一个循环中进行处理
#include <string>
#include <iostream>
#include <vector>
int main()
{
std::string original = "01234567 89abc defghi j";
std::vector< std::string > strings;
// Find space from last index
int lastSpaceIndex = 0;
int spaceIndex = original.find( ' ', lastSpaceIndex );
// Find the number of characters to split
int numCharacters = spaceIndex - lastSpaceIndex;
// Split string ( the second argument is the number of characters to splut out)
std::string tokenizedString = original.substr( lastSpaceIndex, numCharacters );
// Add to vector of strings
strings.push_back( tokenizedString);
// Print result
std::cout << "Space at : " << spaceIndex << std::endl;
std::cout << "Tokenized string : " << tokenizedString << std::endl;
// Find the nextsubstring
// =========================================================================
// Need to increase by 1 since we don't want the space
lastSpaceIndex = spaceIndex + 1;
spaceIndex = original.find( ' ', lastSpaceIndex );
numCharacters = spaceIndex - lastSpaceIndex;
tokenizedString = original.substr( lastSpaceIndex, numCharacters );
strings.push_back( tokenizedString);
std::cout << "Space at : " << spaceIndex << std::endl;
std::cout << "Tokenized string : " << tokenizedString << std::endl;
std::cout << "=====================================\n";
for ( const auto &str : strings )
{
std::cout << "String : " << str << std::endl;
}
}
当没有更多的空格时,
original.find(“”,lastSpaceIndex)
将返回std::npos
为什么不使用std::string
?你能给我举个例子吗
#include <string>
#include <iostream>
#include <vector>
int main()
{
std::string original = "01234567 89abc defghi j";
std::vector< std::string > strings;
// Find space from last index
int lastSpaceIndex = 0;
int spaceIndex = original.find( ' ', lastSpaceIndex );
// Find the number of characters to split
int numCharacters = spaceIndex - lastSpaceIndex;
// Split string ( the second argument is the number of characters to splut out)
std::string tokenizedString = original.substr( lastSpaceIndex, numCharacters );
// Add to vector of strings
strings.push_back( tokenizedString);
// Print result
std::cout << "Space at : " << spaceIndex << std::endl;
std::cout << "Tokenized string : " << tokenizedString << std::endl;
// Find the nextsubstring
// =========================================================================
// Need to increase by 1 since we don't want the space
lastSpaceIndex = spaceIndex + 1;
spaceIndex = original.find( ' ', lastSpaceIndex );
numCharacters = spaceIndex - lastSpaceIndex;
tokenizedString = original.substr( lastSpaceIndex, numCharacters );
strings.push_back( tokenizedString);
std::cout << "Space at : " << spaceIndex << std::endl;
std::cout << "Tokenized string : " << tokenizedString << std::endl;
std::cout << "=====================================\n";
for ( const auto &str : strings )
{
std::cout << "String : " << str << std::endl;
}
}
Space at : 8
Tokenized string : 01234567
Space at : 14
Tokenized string : 89abc
=====================================
String : 01234567
String : 89abc