*it++ = stoi(i);
时间: 2024-04-17 16:29:19 浏览: 10
这行代码是将字符串`i`转换为整数并将其赋值给`it`指向的位置,然后将迭代器`it`向后移动一位。
具体来说,`stoi(i)`将字符串`i`转换为整数,并使用后置递增运算符`++`将迭代器`it`向后移动一位。然后,将转换后的整数赋值给`it`指向的位置。
这行代码的作用是将字符串形式的操作得分转换为整数,并将其存储在`tmp`向量中的当前位置。通过使用迭代器,可以方便地在容器中进行遍历和操作。
如果你还有其他问题,请随时提问。
相关问题
class Solution { public: int calPoints(vector<string>& ops) { vector<int> tmp(ops.size()); auto it{ tmp.begin() }; for (auto& i : ops) { if (i == "+") *it++ = *(it - 1) + *(it - 2); else if (i == "D") *it++ = 2 * *(it - 1); else if (i == "C") it--; else *it++ = stoi(i); } return accumulate(tmp.begin(), it, 0); } };
这段代码是一个C++的解决方案,用于计算一系列操作的得分。它接收一个字符串向量ops作为参数,根据不同的操作进行计算,并返回最终得分。
代码首先创建了一个大小与ops相同的整数向量tmp。然后通过迭代器it来遍历ops中的每个元素。对于每个元素i,代码执行以下操作:
- 如果i等于"+",则将前两个元素相加,并将结果存储在tmp中的当前位置,同时it向后移动一位。
- 如果i等于"D",则将tmp中上一个位置的元素乘以2,并将结果存储在tmp中的当前位置,同时it向后移动一位。
- 如果i等于"C",则将it向前移动一位,即删除上一个操作的得分。
- 否则,将i转换为整数并将其存储在tmp中的当前位置,同时it向后移动一位。
最后,代码使用std::accumulate函数计算tmp中从开始位置到it位置的总和,并将其作为最终得分返回。
请问你还有其他问题吗?
用C++实现下面要求(1)关键字 main if else int return void while 所有的关键字都是小写。 (2)专用符号 = + - * / < < = > > = = = != ; : , { } [ ] ( ) (3)其他标记ID和NUM 通过以下正规式定义其他标记: ID letter(letter | didit)* NUM digit digit* letter a |…| z | A|…|Z digit 0|…| 9 (4)空格有空白、制表符和换行符组成 空格一般用来分隔ID、NUM、专用符号和关键字,词法分析阶段通常被忽略。 2.2各种单词符号对应的种别码 表C.1 各种单词符号的种别码 单词符号 种别码 单词符号 种别码 main 1 [ 28 int 2 ] 29 char 3 { 30 if 4 } 31 for 5 , 32 else 6 : 33 while 7 ; 34 ID 10 > 35 NUM 20 < 36 = 21 >= 37 + 22 <= 38 - 23 == 39 * 24 != 40 / 25 ‘\0’ 1000 ( 26 ERROR -1 ( 27 2.3 词法分析程序的功能 输入:所给文法的原程序字符串。 输出:二元组(syn,token或sum)构成的序列。其中, ●syn为单词种别码。 ●token为存放的单词自身字符串。 ● sum为整型常量。 具体实现时,可以将单词的二元组用结构进行处理。 例如,对源程序 main( ) { int i=10; while( i ) i=i-1; } 的源文件,经词法分析后输出如下序列: (1,main)(26,( ) (27,) ) (30,{) (2,int) (10,i) (21,=) (20,10) (34, ;) (7,while) (26,( ) (10,i) (27,) ) (10,i) (21,=) (10,i) (23,-) (20,1) (34, ;) (31, })
以下是一个基于C++实现的简单词法分析程序,能够识别给定的C语言子集。该程序使用了正则表达式库regex来匹配单词。具体实现中,使用了一个Token结构体来存储识别出的单词及其种别码和值。
```c++
#include <iostream>
#include <string>
#include <regex>
using namespace std;
// Token结构体
struct Token {
int syn; // 单词种别码
string token; // 单词字符串
int value; // 整数值
};
int main() {
// 正则表达式
regex keywords("main|if|else|int|return|void|while");
regex symbols("[=+\\-*/<>]=|!=|==|&&|\\|\\||[{}\\[\\]();,:]");
regex id("[a-zA-Z][a-zA-Z0-9]*");
regex num("[0-9]+");
// 待匹配的字符串
string code = "main() {\n int i = 10;\n while(i) {\n i = i - 1;\n }\n}";
// 用于存储识别出的单词序列
vector<Token> tokens;
// 逐个匹配字符串中的单词
for (sregex_iterator it(code.begin(), code.end(), keywords), end_it; it != end_it; ++it) {
Token token;
token.syn = 1;
token.token = it->str();
tokens.push_back(token);
}
for (sregex_iterator it(code.begin(), code.end(), symbols), end_it; it != end_it; ++it) {
Token token;
string symbol = it->str();
if (symbol == "+") token.syn = 22;
else if (symbol == "-") token.syn = 23;
else if (symbol == "*") token.syn = 24;
else if (symbol == "/") token.syn = 25;
else if (symbol == "<") token.syn = 36;
else if (symbol == "<=") token.syn = 38;
else if (symbol == ">") token.syn = 35;
else if (symbol == ">=") token.syn = 37;
else if (symbol == "==") token.syn = 39;
else if (symbol == "!=") token.syn = 40;
else if (symbol == ";") token.syn = 34;
else if (symbol == "{") token.syn = 30;
else if (symbol == "}") token.syn = 31;
else if (symbol == "[") token.syn = 28;
else if (symbol == "]") token.syn = 29;
else if (symbol == "(") token.syn = 26;
else if (symbol == ")") token.syn = 27;
else if (symbol == ",") token.syn = 32;
else if (symbol == ":") token.syn = 33;
token.token = symbol;
tokens.push_back(token);
}
for (sregex_iterator it(code.begin(), code.end(), id), end_it; it != end_it; ++it) {
Token token;
token.syn = 10;
token.token = it->str();
tokens.push_back(token);
}
for (sregex_iterator it(code.begin(), code.end(), num), end_it; it != end_it; ++it) {
Token token;
token.syn = 20;
token.token = it->str();
token.value = stoi(it->str());
tokens.push_back(token);
}
// 输出识别出的单词序列
for (auto t : tokens) {
cout << "(" << t.syn << "," << t.token;
if (t.syn == 20) {
cout << "," << t.value;
}
cout << ") ";
}
cout << endl;
return 0;
}
```
该程序将给定的C语言子集分为关键字、符号、标识符和数字四类单词,使用Token结构体存储每个识别出的单词,并输出识别出的单词序列。