【问题标题】:c++ append one character from a string to another stringc ++将一个字符从一个字符串附加到另一个字符串
【发布时间】:2015-10-18 13:59:11
【问题描述】:
#include <string>
#include <iostream>

int main()
{
    std::string test("hello world ");
    std::string label("label test");
    test.append(&label[3]);    //----- #1
    std::cout << test << std::endl;
}

对于上面的代码,我期望的是“hello world e”,但实际上它的输出是“hello world el test”。它将位置 3 之后的所有字符附加到我的字符串 test

在#1的位置,如果我不放&符号,会出现编译错误:

str_append.cpp:10:10: error: no matching member function for call to 'append'
    test.append(label[3]);
    ~~~~~^~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1516:19: note: candidate function not viable: no known conversion
      from 'value_type' (aka 'char') to 'const value_type *' (aka 'const char *') for 1st argument; take the address of the argument with &
    basic_string& append(const value_type* __s);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1513:19: note: candidate function not viable: no known conversion
      from 'value_type' (aka 'char') to 'const std::__1::basic_string<char>' for 1st argument
    basic_string& append(const basic_string& __str);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1525:9: note: candidate function template not viable: requires 2
      arguments, but 1 was provided
        append(_InputIterator __first, _InputIterator __last);
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1532:9: note: candidate function template not viable: requires 2
      arguments, but 1 was provided
        append(_ForwardIterator __first, _ForwardIterator __last);
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1515:19: note: candidate function not viable: requires 2
      arguments, but 1 was provided
    basic_string& append(const value_type* __s, size_type __n);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1517:19: note: candidate function not viable: requires 2
      arguments, but 1 was provided
    basic_string& append(size_type __n, value_type __c);
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1514:19: note: candidate function not viable: requires at least 2
      arguments, but 1 was provided
    basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
                  ^
1 error generated.

我可以解决这个问题的唯一方法是将#1 行更改为:

test += label[3];

那么,我想知道这背后的逻辑是什么? label[3] 不能返回单个字符吗?为什么当我只使用test.append(label[3]); 时它会失败。为什么test += label[3];会成功?

提前致谢。

【问题讨论】:

  • 又一个“我没有阅读文档”的问题。世界有什么问题? 投反对票和哭泣

标签: c++ string


【解决方案1】:

你没有附加一个字符;您正在附加一个 C 字符串。该 C 字符串由指针 &amp;label[3] 提供,该指针指向以下数据:{'e','l',' ','t','e','s','t','\0'}

要附加一个字符,您只需传递label[3] 本身。但是,要做到这一点,您需要使用basic_string&amp; append(size_type count, CharT ch),或其方便的等效项(当count==1 时)void push_back(CharT ch)

operator+= 在功能上类似于push_back,这就是为什么当您尝试它时它会起作用的原因。

所以:

#include <string>
#include <iostream>

int main()
{
    std::string test("hello world ");
    std::string label("label test");

    test.append(1, label[3]);  // or...
    test.push_back(label[3]);  // or...
    test += label[3];

    std::cout << test << std::endl;
}

简而言之,阅读文档以了解std::string 的成员有什么,以及如何使用它们。

【讨论】:

    【解决方案2】:

    因为没有采用单个字符的std::basic_string::append() 重载。自己看here

    基本上,std::basic_string::append() 并非旨在将单个字符添加到字符串的后面,而是字符。 std::basic_string::push_back() 就是为此目的而设计的。或者你可以只使用operator +,它适用于单个字符和字符。

    cppreference,每个功能的描述是(强调我的):

    • append():在末尾追加字符
    • push_back():在末尾追加一个字符
    • operator +: 连接两个字符串或一个字符串和一个字符

    【讨论】:

      【解决方案3】:

      label[3] 将返回单个字符(从结构上讲,它的引用),但不幸的是 std::string 没有一个函数 append 需要一个字符。

      test.append('a'); 也失败了。

      std::stringoperator+= 需要一个字符,所以 test += label[3]; 成功。

      【讨论】:

      • 其实是operator +std::basic_string 没有 operator += 过载。
      • @Lingxi N3337basic_stringbasic_string&amp; operator+=(charT c); (21.4.6.1 basic_string::operator+=)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2017-06-06
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多