2016-04-15 9 views
0

私のコードは文字列を受け取り、入力文字列の置換が存在するかどうかを確認しようとします。そのような文字列が1つ存在する場合は、 "no answer"コンパイルしてエラーを表示します。 エラーは:: next_permutation(std :: string &)の呼び出しで一致する関数がありません。 このタスクを実行する正しい方法は何ですか?ここで文字列のC++のnext_permutation

#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 
    } 
} 
+2

あなたはまだエラーメッセージを読むことがありますか?私はあなたの最初のステップとして、stackoverflowに投稿する前にそれをお勧めします。 – user2079303

+0

@ user2079303 'next_permutation(std :: string&)'の呼び出しで一致する関数がありません。 –

+0

コメントだけでなく、質問にも関連情報を入れてください。 – user2079303

答えて

0

機能next_permutationのプロトタイプであり、あなたの呼び出し元の文string ans=next_permutation(s);は、それらのいずれかと一致していません。

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

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

if(next_permutation(s.begin(),s.end())) 
    cout<<s<<endl; 
else 
    cout<<"no answer "<<endl; 

を順列終了を乗り切るかをチェックするために、このようにコードを変更し、あなたがsのすべての順列を印刷したい場合は、これは、この

while(next_permutation(s.begin(),s.end())) 
{ 
    cout<<s<<endl; 
} 

を行う

あなたはタスクを達成するためにロジックを変更する必要があるかもしれませんが、それを行う方法

+0

これを行う正しい方法は何ですか? –

+0

@satya更新されたコードを参照してください –

0

コンパイルエラーは非常に明確である:

no matching function for call to 'next_permutation(std::string&)'| 

コンパイラは何の機能next_permutation(std::string&)が存在しないことを示しています。 documentationを見ると、それは確かに事実です。 2つのオーバーロードがあります。

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

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

いずれもお使いの電話に一致しません。

std::string::beginstd::string::endを使用して、文字列内の文字の範囲にイテレータを適用します。

0

がこの

 // 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; 

      } 
     } 
+0

@RajeevSinghはい –

0

を行うには正しい方法です。このコードは、問題を解決:

#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 
    } 
}