我目前正在尝试为显示不同值的功能的函数编写模板特化.我的代码是:
#include <iostream>
using namespace std;
template <class T>
T myfunc(T x)
{
return x*x;
}
template<>
string myfunc<string>(string ss)
{
return (ss+ss);
}
int main()
{
cout<<myfunc("test")<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译失败:
错误C2784:'std :: _ String_iterator <_Mystr> std :: operator +(_ String_iterator <_Mystr> :: difference_type,std :: _ String_iterator <_Mystr>)':无法推断'std :: _ String_iterator <_Mystr>'的模板参数来自'std :: string'
你能帮我检查一下究竟是什么问题吗?
我目前正在尝试为列表编写循环.我的代码是:
template<typename T>
void Bubblesorting(list<T> & mylist)
{
typename T::const_iterator it1;
typename T::const_iterator it2;
for(it1=mylist.begin();it1!=mylist.end();it1++)
for(it2=mylist.begin();it2!=mylist.end()-(it1-begin());it2++)
if((*(std::next(it2,1))<*it2)
swap((*(std::next(it2,1)),*it2);
cout << *it2 << ' ';
}
Run Code Online (Sandbox Code Playgroud)
编译失败:
error C2958: the left parenthesis '(' was not matched correctly
Run Code Online (Sandbox Code Playgroud)
你能帮我检一下究竟是什么问题吗?我如何为列表元素编写for循环?