连接字符串时出现错误“不匹配‘operator+’”

问题描述

我正在尝试使用此代码反转字符串中的单词:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //_ _ the sky is blue

    string vec;
    getline(cin,vec);
    stack<string> str;

    string temp = "";
    string ans = "";

    for (int i = 0; i < vec.length(); i++)
    {
        if (vec.at(i) == ' ')
        {
            if (temp.length() > 0)
            {
                str.push(temp);
                temp = "";
            }
            else
            {
                temp = temp + vec.at(i);
            }
        }
    }

    //ans = ans + temp;

    while (!str.empty())
    {
        ans = ans + " " + str.pop();
    }

    if (ans.length() != 0 && ans.at(0) == ' ')
        ans = ans.substr(1);

    cout << ans << endl;
}

我在第 33 行收到此错误,告诉 "no match for 'operator+'"

我附上了相关的截图:

enter image description here

请帮忙。

解决方法

pop() 是一个 stack 成员方法,返回类型为 void,它不返回 string,因此它不能被打印,也不能与其他方法连接字符串。

如错误所示,您不能使用 void 运算符为这两种不同类型添加 string+(除非您通过重载 {{1} } 运算符),所以 + 是错误的。

您可以使用:

 ans = ans + " " + str.pop();

因为 while (!str.empty()) { ans = ans + " " + str.top(); str.pop(); } 确实返回一个 top() 对象。


我应该指出,使用 #include <bits/stdc++.h> is badusing namespace std is also not very good,但将它们组合在一起是一场等待发生的灾难。

,

容器适配器 pop 的方法 std::stack 具有返回类型 void。所以这个声明

ans= ans+" "+str.pop();

不正确,编译器会报错。

你需要写一些类似的东西

while(!str.empty()){
    ans= ans+" "+ str.top();
    str.pop();
}

注意这个for循环

 for(int i=0 ; i<vec.length(); i++){
  if(vec.at(i)==' '){
   if(temp.length()>0){
    str.push(temp);
    temp = "";
   }

   else{
    temp = temp + vec.at(i);
   }
  }
}

有一个错误。如果存储在对象 vec 中的字符串不以空格字符结尾,则字符串的最后一个字不会被压入堆栈。

看来您正在尝试执行以下操作。

#include <iostream>
#include <string>
#include <stack>
#include <cctype>

int main() 
{
    std::string s( "   Hello World   " );
    std::stack<std::string> st;
    
    std::cout << "\"" << s << "\"\n";
    
    for ( std::string::size_type i = 0; i < s.size(); )
    {
        std::string tmp;
        
        while ( i < s.size() && isspace( ( unsigned char )s[i] ) )
        {
            tmp += s[i++];
        }
        
        if ( !tmp.empty() ) 
        {
            st.push( tmp );
            tmp.clear();
        }
        
        while ( i < s.size() && !isspace( ( unsigned char )s[i] ) )
        {
            tmp += s[i++];
        }
        
        if ( !tmp.empty() )
        {
            st.push( tmp );
        }
    }
    
    std::string result;
    
    while ( !st.empty() )
    {
        result += st.top();
        st.pop();
    }
    
    std::cout << "\"" << result << "\"\n";
    
    return 0;
}

程序输出为

"   Hello World   "
"   World Hello   "
,

谢谢你们帮助我。这是代码并且运行良好:

#include <bits/stdc++.h>

using namespace std;

int main(){

//_ _ the sky is blue

string vec;
getline(cin,vec);
stack<string>str;
string rs;

string temp="";
string ans= "";

for(int i=0 ; i<vec.length(); i++){
if(vec.at(i)==' '){
   if(temp.length()>0){
    str.push(temp);
    temp = "";
   }
}
   else{
    temp = temp + vec.at(i);
   }
}


     ans = ans + temp;

    while(!str.empty()){
        ans= ans+" "+str.top();
        str.pop();

    }

    if(ans.length() != 0 && ans.at(0) == ' '){
      ans = ans.substr(1);
   }

cout<<ans<<endl;
reverse( ans.begin(),ans.end());
cout<<ans<<endl;
}

以下是每个单词之间只允许一个空格并消除前导和尾随空格的输出: enter image description here