首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >引用作为参数

引用作为参数
EN

Stack Overflow用户
提问于 2018-10-23 09:00:04
回答 2查看 86关注 0票数 1

嗨,我正在尝试更改作为引用传递的vec的内容,我对这个概念非常陌生,无法看出我的代码有什么问题:

代码语言:javascript
复制
std::string pluralize(std::string const& word) {
if (uncountables.count(word) > 0) {
    return word;
}

for (auto const& r : rules) {
    if (r.matches(word)) {
        return r.pluralize(word);
    }
}

// The last rule is fully generic "append s" rule, so we cannot
// get here unless something is seriously wrong.
throw std::runtime_error("Word '" + word + "' did not match any rule");
}

std::vector<std::string> pluralize(std::vector<std::string> const& words) {

for (auto word : words) {
    word = pluralize(word);
    std::cout << word << " word from pluralize called with vec" << std::endl;
}
std::cout << words[0] << " 0 word from pluralize called with vec" << std::endl;
std::cout << words[1] << " 1 word from pluralize called with vec" << std::endl;
return words;
}

当以字符串作为参数调用方法复数时,它将按预期的方式工作:更改传递的word的值。当使用vec调用时,它不会更改传递的字符串的值。这些是我的测试用例:

对于这些测试用例,代码可以正常工作:

代码语言:javascript
复制
SECTION("Respects capitalization") {
    REQUIRE(pluralize("Car") == "Cars");
    REQUIRE(pluralize("Mouse") == "Mice");
    REQUIRE(pluralize("German") == "Germans");
    }

这些测试用例失败:

代码语言:javascript
复制
REQUIRE(
        pluralize({"Car", "Mouse", "German"}) == make_vec({"Cars", "Mice", "Germans"})
    );
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-23 09:43:21

您不能修改words,因为它是const

您没有注意到这一点,因为您没有修改它--在for (auto word : words)中推导出来的类型是std::string,而不是std::string&

例如,您可以复制输入并修改副本:

代码语言:javascript
复制
std::vector<std::string> pluralize(std::vector<std::string> const& words)
{
    std::vector<std::string> plurals = words;
    for (auto& word : plurals) {
        word = pluralize(word);
    }
    return plurals;
}

或者在一个循环中收集多个单词:

代码语言:javascript
复制
std::vector<std::string> pluralize(std::vector<std::string> const& words)
{
    std::vector<std::string> plurals;
    for (const auto& word : words) {
        plurals.push_back(pluralize(word));
    }
    return plurals;
}

或者使用std::transform

代码语言:javascript
复制
std::vector<std::string> pluralize(std::vector<std::string> const& words)
{
    std::vector<std::string> plurals;
    std::transform(words.begin(), words.end(), std::back_inserter(plurals), pluralize);
    return plurals;
}

或者其他解决方案..。

票数 0
EN

Stack Overflow用户

发布于 2018-10-23 09:30:46

问题在于:

代码语言:javascript
复制
for (auto word : words) {
    word = pluralize(word);
}

这里的变量word是一个字符串值。您正在更改该值,但这不会改变向量中的单词。尝试使用引用:

代码语言:javascript
复制
for (auto& word : word) {
    word = pluralize(word);
}

auto不推导引用。

编辑:,您不能修改向量中的元素,因为可以将它作为对const的引用。解决这个问题的方法之一就是取一份矢量的副本。最简单的方法是修改函数的签名以传递值:

代码语言:javascript
复制
std::vector<std::string> pluralize(std::vector<std::string> words) {
    for (auto& word : words) {
        word = pluralize(word);
    }
    return words;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52945088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档