简体   繁体   English

字符串的排列-错误:没有匹配的函数来调用&#39;std :: set <char> :: insert(std :: string&)&#39;

[英]Permutations of a string --error: no matching function for call to ‘std::set<char>::insert(std::string&)’

I am not a professional c++ programmer. 我不是专业的c++程序员。 I have tried to write a program to display all possible permutations of a given input string assuming if the string contains duplicate characters. 我尝试编写一个程序来显示给定输入字符串的所有可能排列,并假设该字符串是否包含重复字符。 This the code I have written so far: 到目前为止,我已经编写了以下代码:

Latest update of my code (permutation.cpp): 我的代码(permutation.cpp)的最新更新:

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <list>
#include <set>
#include <iterator>
#include <sstream>
#include <cstdlib>
using namespace std;

//factorial function 
int factorial(int n){
    if (n==0) return 1;
    else{
       return n*factorial(n-1);
    }
}


int main (int argc, char *argv[]) {
   srand ( time(NULL) ); //initialize the random seed
   char* text;
   std::set<char> mySwapList;

   if ( argc < 1 )
   {
       cout << endl << "Please write a word in the following of the command line" << endl << endl;
       return 1;
   }
   else if ( argc != 2 ) // argc should be 2 for correct execution
   {  // We print argv[0] assuming it is the program name
       strcpy (text,argv[1]);
       cout<<endl<<"usage: "<< argv[0] <<" to compute all the permutations \n"<<endl;
       return 1;
   }   
   int ss=sizeof(text);
   int length = ss;
   int k=0;
   cout << "length of the word:"<<endl<< length<<endl;
   int total=factorial(length);
   while (k< total)
   { 
      char arr[ss];
      int index=0;
      stringstream ssin(text);
      while (ssin.good() && index<ss){
      ssin>>arr[index];
      ++index;
   }
      std::list<char> word(arr, arr+ss); 
      std::list<char> mylist;
      unsigned int j=0;
      while (j < length)
      {
           int n=word.size();
           int RandIndex = rand() % n;
           std::list<char>::iterator vi= word.begin();
           std::advance(vi,RandIndex);      
           std::list<char>::iterator iter= mylist.begin();
           mylist.insert(iter,*vi);
           word.remove(*vi);
           j++;
      }
      char str[ss];
      int ii=0;
      for (std::list<char>::iterator ix=mylist.begin(); ix!=mylist.end(); ++ix)
      {
          str[ii]=*ix;
          ii++;
      }
      string newWord = string(str);
      for (std::set<char>::iterator iss=mySwapList.begin(); iss!=mySwapList.end(); ++iss)
      {
      string w(1,*iss);
          if (newWord != w)
          {
             mySwapList.insert(newWord);
             k++;
      }
      }
   }
   //Loop for printing the list
   for(std::set<char>::iterator it = mySwapList.begin(); it != mySwapList.end(); ++it)
        cout << *it << " ";
    cout << endl;
   return 0;
}

I get aa good deal of error messages when I compile the code, including: 编译代码时,我收到很多错误消息,包括:

Updated errors: 更新的错误:

permutation.cpp: In function ‘int main(int, char**)’:
permutation.cpp:82:39: error: no matching function for call to ‘std::set<char>::insert(std::string&)’
              mySwapList.insert(newWord);
                                       ^
permutation.cpp:82:39: note: candidates are:
In file included from /usr/include/c++/4.8/set:61:0,
                 from permutation.cpp:9:
/usr/include/c++/4.8/bits/stl_set.h:460:7: note: std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = char; _Compare = std::less<char>; _Alloc = std::allocator<char>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<char>; std::set<_Key, _Compare, _Alloc>::value_type = char]
       insert(const value_type& __x)
       ^
/usr/include/c++/4.8/bits/stl_set.h:460:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const value_type& {aka const char&}’
/usr/include/c++/4.8/bits/stl_set.h:497:7: note: std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = char; _Compare = std::less<char>; _Alloc = std::allocator<char>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<char>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<char>; std::set<_Key, _Compare, _Alloc>::value_type = char]
       insert(const_iterator __position, const value_type& __x)
       ^
/usr/include/c++/4.8/bits/stl_set.h:497:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/4.8/bits/stl_set.h:517:2: note: template<class _InputIterator> void std::set<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = char; _Compare = std::less<char>; _Alloc = std::allocator<char>]
  insert(_InputIterator __first, _InputIterator __last)
  ^
/usr/include/c++/4.8/bits/stl_set.h:517:2: note:   template argument deduction/substitution failed:
permutation.cpp:82:39: note:   candidate expects 2 arguments, 1 provided
              mySwapList.insert(newWord);

I can not figure out why I got the above errors. 我不知道为什么会出现上述错误。 Any suggestion? 有什么建议吗?

Based on the question title you want to convert string to list . 您要根据问题标题将string转换为list You can't do it directly but you can use string iterator: 您不能直接执行,但可以使用string迭代器:

#include <string>
#include <list>

int main(int argc, _TCHAR* argv[])
{
    std::string strTest = "hello!";
    std::list<char> list(strTest.begin(), strTest.end());

    return 0;
}

Edit: But based on your code you don't need list at all. 编辑:但是根据您的代码,您根本不需要list You can use std::string everywhere. 您可以在任何地方使用std::string It has insert and [] stuff. 它具有insert[]内容。 It is almost the same as vector<char> but with additional functionality. 它与vector<char>几乎相同,但具有附加功能。

You used undefined class members. 您使用了未定义的类成员。 Try strcpy_s() to char* arrays. 尝试将strcpy_s()char*数组。

Using vector as a container for strings at the end of the code for mySwapList solves the problem of accepting strings as well as being easy to access each component of the vector . mySwapList代码的末尾使用vector作为字符串的容器可以解决接受strings的问题,并且易于访问vector每个组件。 Here is the debugged and working version of the original question: 这是原始问题的调试和工作版本:

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <list>
#include <set>
#include <iterator>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
using std::vector;

//factorial function 
int factorial(int n){
    if (n==0) return 1;
    else{
       return n*factorial(n-1);
    }
}


int main () {
   srand ( time(NULL) ); //initialize the random seed
   std::string text;
   vector<std::string> mySwapList;
   std::ostream_iterator<char> output(cout," ");
   cout << endl << "Please write a word in the following of the command line" << endl << endl;
   cin >>text;   
   int ss=text.size();
   int k=0;
   cout << "length of the word:" << endl << ss<<endl<<"input value:\n" <<text<< endl;
   int total=factorial(ss);
   cout << "The number of final permutations :\n"<<total<<endl;

   while (k< total)
   { 
      char arr[ss];
      int index=0;
      stringstream ssin(text);
      while (ssin.good() && index<ss){
    ssin>>arr[index];
    ++index;
      }
      std::list<char> word;
      //insert items from arr into word list
      word.insert(word.begin(),arr, arr+ss);
      //A tool to print lists

      cout<<"word contains:\n "<<endl;
      std::copy(word.begin(),word.end(),output);
      cout << endl;
      std::list<char> mylist;
      unsigned int j=0;
      while (j < ss)
      {
    int n=word.size();
    int RandIndex = rand() % n;
    std::list<char>::iterator vi= word.begin();
    std::advance(vi,RandIndex);
    word.erase(vi);

    std::list<char>::iterator iter= mylist.begin();
    mylist.insert(iter,*vi);
    j++;
      }
      cout << "constructed permuted word:"<<endl;
      std::copy(mylist.begin(),mylist.end(),output);
      cout << endl;
      char str[ss];
      str[ss]='\0';
      int ii=0;
      for (std::list<char>::iterator ix=mylist.begin(); ix!=mylist.end(); ++ix)
      {
      str[ii]=*ix;
          ii++;
      }
      string newWord = string(str);
      cout << "New Word :"<< endl << newWord <<" size of string:\n"<< (sizeof(str)/sizeof(*str)) << endl;

      if (k==0)
      {
         mySwapList.push_back(newWord);
         k++;

      }
      else     
      {
    int flag=0;
        for (int i=0;i<mySwapList.size();i++)
        {
          if (mySwapList[i]==newWord)
         flag=1;
        }
        if (flag!=1)
    {
         mySwapList.push_back(newWord);
         k++;    
    }
      }  
   }
   //Loop for printing the vector mySwapList
   for(unsigned int i=0; i<mySwapList.size();i++)
      cout << mySwapList[i] << " ";
   cout << endl;

   return 0;
}

暂无
暂无

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

相关问题 错误:没有匹配函数来调用...(std :: string&) - error: no matching function for call to…(std::string&) 为什么这个编译错误? - 没有匹配函数来调用&#39;std :: basic_ofstream <char> ::打开(的std :: string&)” - why this compiler error? - no matching function for call to 'std::basic_ofstream<char>::open(std::string&)' 错误:没有匹配函数可调用&#39;isupper(std :: string&)&#39;| - error: no matching function for call to 'isupper(std::string&)'| 错误:没有用于调用“getline(FILE*&amp;, std::string&amp;)”的匹配函数 - error: no matching function for call to 'getline(FILE*&, std::string&)' 错误:没有匹配的函数调用&#39;std::basic_ifstream<char> ::basic_ifstream(std::__cxx11::string&amp;)&#39; - error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::__cxx11::string&)' 没有用于调用`std :: basic_ofstream的匹配函数 <char, std::char_traits<char> &gt; :: basic_ofstream(的std :: string&)” - no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)' C ++错误:无法调用&#39;(std :: string {aka std :: basic_string <char> })(std :: string&)&#39; - C++ Error: no match for call to ‘(std::string {aka std::basic_string<char>}) (std::string&)’ 没有匹配的 function 调用 'std::basic_ifstream<char> ::basic_ifstream(std::__cxx11::string&amp;)' ifstream myfile(filename);</char> - no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(std::__cxx11::string&)’ ifstream myfile(filename); 使用 getline 给出错误:没有匹配的 function 调用 'getline(std::istream&amp;, const string&amp;)' - using getline gives error: no matching function for call to ‘getline(std::istream&, const string&)’ “没有匹配函数来调用'async(std :: launch,<unresolved overloaded function type>,std :: string&)'” - “no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM