简体   繁体   English

字符串未正确复制并显示错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }'</char>

[英]string not copying correctly and showing error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'

I was trying to make Substrings of exactly half the length of original string and then sorting them in order of their ASCII codes and finally checking their equality by using strcmp() library function.我试图使子字符串的长度恰好是原始字符串的一半,然后按它们的 ASCII 码顺序对它们进行排序,最后使用 strcmp() 库 function 检查它们的相等性。 (That is i was trying to check if the parent string can be divided in two equal halves.) So here is what I did: (也就是说,我试图检查父字符串是否可以分成相等的两半。)所以这就是我所做的:

  1. Took input of parent string from user by cin.通过 cin 从用户那里输入父字符串。

  2. Then I used variable hlength to store the value of half of the length of original string.然后我使用变量 hlength 来存储原始字符串长度的一半的值。

  3. Now I declared two strings of equal lengths (equal to hlength ) so that first half of string is copied to substring named "s1" and the rest half is copied to substring "s2".现在我声明了两个长度相等的字符串(等于 hlength ),以便将字符串的前半部分复制到名为“s1”的 substring 并将 rest 的一半复制到 substring “s2”。 i used for loops to copy the elements of the string.我使用 for 循环来复制字符串的元素。

  4. To check whether the strings are correctly copied or not I printed each element of string just after copying them by using cout<< as you can see in the attached code.为了检查字符串是否被正确复制,我在使用cout<<复制字符串后打印了字符串的每个元素,正如您在附加代码中看到的那样。

  5. Till now everything seemed alright as the result of individual elements when being printed were correct.直到现在一切似乎都很好,因为打印时各个元素都是正确的。

  6. But:!但:! here comes a situation which I never faced before: When I tried to print complete string using cout<< The output was blank.这是我以前从未遇到过的情况:当我尝试使用cout<<打印完整的字符串时,output 是空白的。

  7. Later instead of printing the strings I just tried comparing them with the strcmp() function but it resulted in error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'const char ' for argument '1' to 'int strcmp(const char*, const char*)'*后来我没有打印字符串,而是尝试将它们与 strcmp() function 进行比较,但导致错误: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'const char ' for参数 '1' 到 'int strcmp(const char*, const char*)'*

#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define llt long long int
using namespace std;
int main()
{
    int length,hlength,i,result;
    string s;
    cin>>s;
    length =  s.length();
    hlength = length/2;
    string s1,s2;
    for(i = 0; i< hlength;i++)
    {
        s1[i] = s[i];
        cout<<s1[i]<<endl; //each element gets printed successfully
    }
    for(i = 0; i< hlength ; i++)
    {
        s2[i] = s[length -1 - i];
        cout<<s2[i]<<endl; //the elements are printed successfully, but obviously in the reverse order( order not matters)
    }
    cout<<s1<<endl<<s2; // no strings printed
    sort(s1.begin(),s1.end());
    sort(s2.begin(),s2.end());

    result = strcmp(s1 , s2);   //ERROR
    cout<<result<<endl;

    return 0;
}

PS: The original string is even in length. PS:原始字符串的长度是偶数。

Please help me know why the string is not printed but individual elements are printed correctly.请帮助我知道为什么没有打印字符串但正确打印了各个元素。 And what does that error means?这个错误是什么意思?

strcmp expects char pointers - this is what your error message tells you. strcmp需要 char 指针 - 这就是您的错误消息告诉您的内容。 There is string::compare or operator == which you can use without this error.string::compare或 operator ==您可以使用它们而不会出现此错误。 See: Stackexchange Topic and cppreference .请参阅: Stackexchange 主题cppreference

For completeness you could also convert your std::string s to char pointers with c_str() method of your strings, but this is kind of ugly.为了完整起见,您还可以使用字符串的c_str()方法将std::string转换为 char 指针,但这有点难看。

You see the real values, but you experience undefined behaviour.你看到了真正的价值,但你体验到了不确定的行为。 s1 and s2 are intialized with zero length. s1s2初始化为零长度。 Operator [pos] definition:运算符[pos]定义:

  • If pos is less than the string length, the function never throws exceptions (no-throw guarantee).如果 pos 小于字符串长度,则 function 永远不会抛出异常(无抛出保证)。

  • If pos is equal to the string length, the const-version never throws exceptions (no-throw guarantee).如果 pos 等于字符串长度,则 const-version 永远不会抛出异常(无抛出保证)。

  • Otherwise, it causes undefined behavior.否则,它会导致未定义的行为。

    The last point is what you experience - Maybe you write to a part of memory which does not belong to the string, but can access it in the same scope still.最后一点是您所经历的 - 也许您写入 memory 的一部分,它不属于字符串,但仍然可以在同一个 scope 中访问它。 But when you try to cout the string, there is nothing written in the strings memory block.但是当您尝试计算字符串时,字符串 memory 块中没有写入任何内容。 But who knows, its undefined behavior, anything can happen.但谁知道,它的未定义行为,任何事情都可能发生。

Section Exception safety in:异常安全

http://www.cplusplus.com/reference/string/string/operator[]/ http://www.cplusplus.com/reference/string/string/operator[]/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取“错误:无法转换 'std::__cxx11::string* {aka std::__cxx11::basic_string<char> *}' 到 'const char*' 错误,同时将路径作为参数传递</char> - Getting "error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'const char*' error while passing a path as argument 尝试生成随机字母错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'char' 在分配</char> - Trying to generate a random letter error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment c++ 无法将 'std::string' {aka 'std::__cxx11::basic_string'} 转换为 'std::string (*)[3]' {aka 'std::__cxx11::basic_string (*)[3] '} - c++ cannot convert ‘std::string’ {aka ‘std::__cxx11::basic_string’} to ‘std::string (*)[3]’ {aka ‘std::__cxx11::basic_string (*)[3]’} 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 错误:无法转换 'std::__cxx11::basic_string<char> ::迭代器'</char> - error: cannot convert ‘std::__cxx11::basic_string<char>::iterator’ OSX“错误:无法转换&#39;const std :: __ cxx11 :: basic_string”是什么意思 <char> “ 意思? - OSX What does “error: cannot convert 'const std::__cxx11::basic_string<char>” mean? 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument 错误:无法从“向量”转换“标签” <std::vector<std::__cxx11::basic_string<char> &gt;&gt;' 到 ' 向量<std::__cxx11::basic_string<char> &gt;' </std::__cxx11::basic_string<char></std::vector<std::__cxx11::basic_string<char> - error: could not convert 'tab' from 'vector<std::vector<std::__cxx11::basic_string<char> >>' to 'vector<std::__cxx11::basic_string<char>>' (std::__cxx11::string) [T = std::__cxx11::basic_string<char> ; std::__cxx11::string = std::__cxx11::basic_string<char> ] 不能重载 - (std::__cxx11::string) [with T = std::__cxx11::basic_string<char>; std::__cxx11::string = std::__cxx11::basic_string<char>] cannot be overloaded 不匹配调用 '(std::string {aka std::__cxx11::basic_string<char> }) ()'</char> - no match for call to '(std::string {aka std::__cxx11::basic_string<char>}) ()'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM