Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在使用String.resize()之后,为什么';字符串的大小不会改变吗?_C++_String_Resize - Fatal编程技术网

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)
,并思考额外的
功能。