小编use*_*486的帖子

如何创建c ++模板?

我目前正在尝试为显示不同值的功能的函数编写模板特化.我的代码是:

#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 :: _ St​​ring_iterator <_Mystr> std :: operator +(_ String_iterator <_Mystr> :: difference_type,std :: _ St​​ring_iterator <_Mystr>)':无法推断'std :: _ St​​ring_iterator <_Mystr>'的模板参数来自'std :: string'

你能帮我检查一下究竟是什么问题吗?

c++ templates

1
推荐指数
1
解决办法
89
查看次数

如何为列表编写for循环?

我目前正在尝试为列表编写循环.我的代码是:

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循环?

c++ for-loop function list

-3
推荐指数
1
解决办法
120
查看次数

标签 统计

c++ ×2

for-loop ×1

function ×1

list ×1

templates ×1