C++ c++;在弦上

C++ c++;在弦上,c++,C++,我的代码接受一个字符串并尝试检查输入字符串是否存在排列。如果存在一个这样的字符串,则打印它,否则打印“无答案”。但我的代码未编译并显示错误。 错误是::调用“下一个置换(std::string&)”时没有匹配函数| 完成这项任务的正确方法是什么 #include <bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ string s;

我的代码接受一个字符串并尝试检查输入字符串是否存在排列。如果存在一个这样的字符串,则打印它,否则打印“无答案”。但我的代码未编译并显示错误。 错误是::调用“下一个置换(std::string&)”时没有匹配函数| 完成这项任务的正确方法是什么

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        string ans=next_permutation(s);// do permutation and store it in string ans
        if(ans==s)// if there is no permutation possible original and ans will be same
            cout<<"no answer "<<endl;
        else
            cout<<ans<<endl;// else print ans
    }
}
#包括
使用名称空间std;
int main(){
int t;
cin>>t;
而(t--){
字符串s;
cin>>s;
string ans=next_permutation(s);//进行置换并将其存储在string ans中
if(ans==s)//如果没有排列,则可能的原始和ans是相同的

cout这里是函数的原型
next_permutation
,您的调用语句
string ans=next_permutation;
与它们都不匹配

template <class BidirectionalIterator>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator last);

template <class BidirectionalIterator, class Compare>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator last, Compare comp);

这是一种方法,尽管您可能必须更改逻辑才能完成任务

编译错误非常明显:

编译器告诉您不存在函数
next\u permutation(std::string&)
。如果查看,您会发现确实存在这种情况。有两种重载:

template< class BidirIt >
bool next_permutation( BidirIt first, BidirIt last );

template< class BidirIt, class Compare >
bool next_permutation( BidirIt first, BidirIt last, Compare comp );
模板
bool next_置换(BidirIt first,BidirIt last);
模板
bool next_排列(BidirIt first,BidirIt last,comp比较);
它们都不符合你的要求


使用
std::string::begin
std::string::end
获取字符串中字符范围的迭代器。

这是正确的方法

       // thanks to rajeev's code
       #include <bits/stdc++.h>
         using namespace std;
         int main(){
         int t;
         cin>>t;
         while(t--){
           string s;
           cin>>s;
           bool ans=next_permutation(s.begin(),s.end());
           if(!ans)
           cout<<"no answer "<<endl;
           else
           cout<<s<<endl;

            }
         }
//多亏了拉吉耶夫的代码
#包括
使用名称空间std;
int main(){
int t;
cin>>t;
而(t--){
字符串s;
cin>>s;
bool ans=next_置换(s.begin(),s.end());
如果(!ans)

cout此代码还解决了以下问题:

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        string original_s=s; // store the input string in a variable as original_s
        next_permutation(s.begin(),s.end());// do permutation and s will be changed if permutation occurs
        if(s==original_s)// if there is no permutation possible original and s will be same
            cout<<"no answer "<<endl;
        else
            cout<<s<<endl;// else print ans
    }
}
#包括
使用名称空间std;
int main(){
int t;
cin>>t;
而(t--){
字符串s;
cin>>s;
string original_s=s;//将输入字符串作为original_s存储在变量中
下一个_置换(s.begin(),s.end());//进行置换,如果发生置换,s将被更改
if(s==original\u s)//如果没有排列,那么可能的original和s将是相同的

您是否已经阅读了错误消息?我建议您在发布stackoverflow之前,先执行第一步。@user2079303没有匹配的函数来调用“next_permutation(std::string&)”|将相关信息也放入问题中,而不仅仅是在注释中。
template< class BidirIt >
bool next_permutation( BidirIt first, BidirIt last );

template< class BidirIt, class Compare >
bool next_permutation( BidirIt first, BidirIt last, Compare comp );
       // thanks to rajeev's code
       #include <bits/stdc++.h>
         using namespace std;
         int main(){
         int t;
         cin>>t;
         while(t--){
           string s;
           cin>>s;
           bool ans=next_permutation(s.begin(),s.end());
           if(!ans)
           cout<<"no answer "<<endl;
           else
           cout<<s<<endl;

            }
         }
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        string original_s=s; // store the input string in a variable as original_s
        next_permutation(s.begin(),s.end());// do permutation and s will be changed if permutation occurs
        if(s==original_s)// if there is no permutation possible original and s will be same
            cout<<"no answer "<<endl;
        else
            cout<<s<<endl;// else print ans
    }
}