小编vat*_*sal的帖子

为什么模数运算符给出了错误的答案?

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main(){
    stringstream ss;
    ss << 32;
    string str = ss.str();
    cout << str << endl
         << str[0] << endl
         << str[1] <<endl
         << str[0]%10;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

32
3
2
1

相反,最后一行应为3,因为3%10 = 3.

c++ string io stringstream mod

-5
推荐指数
1
解决办法
449
查看次数

标签 统计

c++ ×1

io ×1

mod ×1

string ×1

stringstream ×1