2015-10-18 415 views
0
#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之后的所有字符附加到我的字符串testC++将一个字符从一个字符串追加到另一个字符串

在位置#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];成功?

在此先感谢。

+0

又一个“我没看过文档”的问题。这个世界有什么问题? * downvotes和哭泣* –

回答

2

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

要追加一个你刚传递的字符label[3]本身。但是,要执行此操作,您需要使用basic_string& 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有,以及如何使用它们。

1

因为没有std::basic_string::append()重载需要一个字符。亲自看看here

基本上,std::basic_string::append()不是为字符串的后面添加单个字符而是字符。 std::basic_string::push_back()是专为此目的而设计的。或者,您可以使用适用于单个字符和字符的operator +

cppreference,每个功能的描述是(重点煤矿):

  • append():追加字符到端
  • push_back():追加字符到端
  • operator + :连接两个字符串或一个字符串和一个字符
0

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

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

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

+0

其实,它是'operator +'。 'std :: basic_string'没有'operator + ='过载。 – Lingxi

+0

@Lingxi [N3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf)说'basic_string'有'basic_string&operator + =(charT c); '(21.4.6.1 basic_string :: operator + =) – MikeCAT

+0

是的。感谢您的更正。 – Lingxi