【发布时间】:2015-02-16 21:23:09
【问题描述】:
我编写了以下 C++ 代码来生成输入字符串的下一个逆字母顺序。但是我收到错误消息:没有调用“反向”的匹配函数:候选模板被忽略:推断参数的冲突类型 '_BidirectionalIterator' ('std::__1::basic_string' vs. 'int') 反向(_BidirectionalIterator __first,_BidirectionalIterator __last)。 我无法理解错误消息,也不知道如何调整它。有人可以帮我吗?谢谢!
#include<iostream>
#include<string>
using namespace std;
string gen(string A,int n){
int i, j;
for(i= n-1;(i > 0 && A[i-1]<A[i]);i--)
; // empty statement
if (i == 0)
return 0;
for (j = i+1; j < n && A[i-1] > A[j]; j++)
; // empty statement
swap(A[i-1],A[j-1]); // swap values in the two entries
string subline =A.substr(i,n-i);
subline=reverse(subline,n-i);
A=A.substr(0,i-1)+subline;
return A;
}
void swap(int &a,int &b)
{
int temp=b;
b=a;
a=temp;
}
string reverse(string k,int length)
{
for(int m=0;m<length/2;m++)
{
char temp=k[length-1-m];
k[length-1-m]=k[m];
k[m]=temp;
}
return k;
}
int main(void)
{
cout<<"Please enter a string"<<endl;
string arrayperm;
cin>>arrayperm;
int length=arrayperm.length();
string newone=gen(arrayperm,length);
cout<<"The new array is: "<<newone<<endl;
return 0;
}
【问题讨论】:
-
为什么人们坚持重写
std::swap(很糟糕)。 -
不要说
using namespace std;会和standard library names发生冲突。 -
你是
using namespace std;,然后定义swap和reverse这两个标准库中定义的函数,你会出问题的。但更重要的是,您在定义之前使用您的reverse,请参阅我上面的链接。 -
我的眼睛!请阅读:coding style by mozilla.