如何在c++中引用对象用于范围循环

发布于 2025-01-26 18:06:05 字数 654 浏览 1 评论 0原文

在以下代码中,[id,name]是const引用。但是,StudentMap是非const的。用户可以更改循环中StudentMap的值。 我想问是否有一种方法可以使StudentMap也可以进行const。谢谢。

#include <iostream>
#include <string>
#include <map>


int main() {
    std::map<int, std::string> studentMap;
    studentMap[1] = "Tom";
    studentMap[7] = "Jack";
    studentMap[15] = "John";

    for (const auto& [id, name] : studentMap) {
        studentMap.at(id) += "test";
    }

    for (const auto& [id, name]: studentMap) {
        std::cout << id << " " << name << "\n";
    }

    return 0;
}

In the following code, [id, name] is a const reference. However, studentMap is non-const. The user can change the value of studentMap in the loop.
I want to ask whether there is a way to make the StudentMap also const. Thanks.

#include <iostream>
#include <string>
#include <map>


int main() {
    std::map<int, std::string> studentMap;
    studentMap[1] = "Tom";
    studentMap[7] = "Jack";
    studentMap[15] = "John";

    for (const auto& [id, name] : studentMap) {
        studentMap.at(id) += "test";
    }

    for (const auto& [id, name]: studentMap) {
        std::cout << id << " " << name << "\n";
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有你我更好 2025-02-02 18:06:05

不,我认为不可能更改变量的类型。

如果要避免修改StudentMap的意外错误,则可以将逻辑拉到单独的功能中,并使用const Ref参考studentmap

#include <iostream>
#include <string>
#include <map>

void displayStudentMap(const auto& studentMap) {
    for (const auto& [id, name] : studentMap) {
        // compilation error
        studentMap.at(id) += "test";
    }

    for (const auto& [id, name]: studentMap) {
        std::cout << id << " " << name << "\n";
    }

}

int main() {
    std::map<int, std::string> studentMap;
    studentMap[1] = "Tom";
    studentMap[7] = "Jack";
    studentMap[15] = "John";

    displayStudentMap(studentMap);
}

No I don't think it is possible to change the type of a variable.

If you want to avoid unexpected mistake of modifying studentMap, you could pull the logic into a separate function, and refer studentMap with a const ref:

#include <iostream>
#include <string>
#include <map>

void displayStudentMap(const auto& studentMap) {
    for (const auto& [id, name] : studentMap) {
        // compilation error
        studentMap.at(id) += "test";
    }

    for (const auto& [id, name]: studentMap) {
        std::cout << id << " " << name << "\n";
    }

}

int main() {
    std::map<int, std::string> studentMap;
    studentMap[1] = "Tom";
    studentMap[7] = "Jack";
    studentMap[15] = "John";

    displayStudentMap(studentMap);
}
半寸时光 2025-02-02 18:06:05

这样:

const std::map<int, std::string> studentMap {
    std::make_pair(1, "Tom"),
    std::make_pair(7, "Jack"),
    std::make_pair(15, "John")
};

或使用C ++ 11均匀初始化:

const std::map<int, std::string> studentMap {
        { 1, "Tom" },
        { 7, "Jack" },
        { 15, "John" }
};

This way:

const std::map<int, std::string> studentMap {
    std::make_pair(1, "Tom"),
    std::make_pair(7, "Jack"),
    std::make_pair(15, "John")
};

or this with C++11 uniform initialization:

const std::map<int, std::string> studentMap {
        { 1, "Tom" },
        { 7, "Jack" },
        { 15, "John" }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文