您的數據是否提供了結束符?
此實現使用「\ 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