C++ 在使用String.resize()之后,为什么';字符串的大小不会改变吗?
我使用下面的代码块希望将保留字表的每个元素的大小设置为C++ 在使用String.resize()之后,为什么';字符串的大小不会改变吗?,c++,string,resize,C++,String,Resize,我使用下面的代码块希望将保留字表的每个元素的大小设置为8: for(auto s : tableOfReservedWords) { s.resize(8); cout<< "S is " << s << " ,Size is "<< s.size() << endl; } 现在我对这个结果感到困惑。很明显,我使用了public member函数resize(),但调用函数check()时,该用法
8
:
for(auto s : tableOfReservedWords) {
s.resize(8);
cout<< "S is " << s << " ,Size is "<< s.size() << endl;
}
现在我对这个结果感到困惑。很明显,我使用了public member函数resize()
,但调用函数check()
时,该用法不起作用。
有没有精通C++的人愿意帮助我?我只是个完全的新手。
提前谢谢
这是我的全部代码:
#include <iostream>
#include <vector>
#include "string"
using namespace std;
vector<string> tableOfReservedWords {"VAR", "INTEGER", "BEGIN", "END", "WHILE", "IF", "THEN", "ELSE", "DO"};
void check() {
for(auto s : tableOfReservedWords) {
//s.resize(8);
cout<< "S is " << s << " ,Size is "<< s.size() << endl;
}
}
int main(int argc, char *argv[]) {
for(auto s : tableOfReservedWords) {
s.resize(8);
cout<< "S is " << s << " ,Size is "<< s.size() << endl;
}
cout<< "---------------------" << endl;
check();
}
#包括
#包括
#包括“字符串”
使用名称空间std;
保留字{“VAR”、“INTEGER”、“BEGIN”、“END”、“WHILE”、“IF”、“THEN”、“ELSE”、“DO}的向量表;
无效检查(){
用于(自动s:保留字表){
//s、 调整大小(8);
cout您在main
中的循环正在调整字符串副本的大小:
for(auto s : tableOfReservedWords)
s.resize(8); // Resizes 's', which is a copy of the original string
它的工作原理与
std::string word = "VAR";
void check()
{
std::cout << word.size();
}
int main()
{
std::string w = word;
w.resize(8);
check();
}
main
中的循环正在调整字符串副本的大小:
for(auto s : tableOfReservedWords)
s.resize(8); // Resizes 's', which is a copy of the original string
它的工作原理与
std::string word = "VAR";
void check()
{
std::cout << word.size();
}
int main()
{
std::string w = word;
w.resize(8);
check();
}
尝试查找(自动&s:tableOfReservedWords)
,并思考额外的和功能。尝试查找(自动&s:tableOfReservedWords)
,并思考额外的和功能。