您的数据是否提供了结束符?
此实现使用“\ 0”作为最终分隔符和不检查错误的数据(这可能吗?)
void exec_function(char operation, int data) {
switch(operation) {
//...
}
}
std::string input = "A123A983A828";
char operation=0;
int data=0;
int index=-1;
// for all characters
for (int i = 0; i < input.length(); i++) {
// current character is a capital letter or '\0'
if (input[i] > 64 && input[i] < 91 || input[i] == '\0') {
// if we already found a letter
//between 2 letters are only numbers
if (index != -1) {
int base=1;
// this is just a simple str2int implementation
for (int j=i-1; j != index; j--) {
data += (index[j]-48)*base;
base*=10;
}
// operation and data are available
exec_function(operation, data);
}
// clear old data
data=0;
// set new operation
operation=input[i];
// update position of last found operation
index = i;
}
}
什么是你的问题?你写了什么代码? –
[标准字符串类](http://en.cppreference.com/w/cpp/string/basic_string)具有将字符串拆分为[子字符串]的功能(http://en.cppreference.com/w/CPP /串/ basic_string的/ SUBSTR)。 [将字符串转换为整数]很容易(http://en.cppreference.com/w/cpp/string/basic_string/stol)。 –
但是,只有当我有一个单词时才会有效。在这里我有几个字母数字短语,而且我不知道字符串中每个字母的索引。 – VVSTITAN