我不是一名专业的c++
程序员。我试图编写一个程序来显示给定输入字符串的所有可能的排列,假设字符串包含重复的字符。这是我到目前为止编写的代码:
我的代码的最新更新(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;
}
编译代码时收到大量错误消息,包括:
更新错误:
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);
我无法弄清楚为什么会出现上述错误。有什么建议吗?
答案 0 :(得分:2)
根据您要将string
转换为list
的问题标题。
您无法直接执行此操作,但可以使用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;
}
编辑:但根据您的代码,您根本不需要list
。您可以在任何地方使用std::string
。它有insert
和[]
个东西。它与vector<char>
几乎相同,但具有其他功能。
答案 1 :(得分:0)
您使用了未定义的类成员。
试试strcpy_s()
到char*
数组。
答案 2 :(得分:0)
在vector
代码末尾使用mySwapList
作为字符串容器解决了接受strings
以及轻松访问{{1}的每个组件的问题}}。以下是原始问题的调试和工作版本:
vector