我一直在努力理解模板专业化.为什么会产生错误(specialization of 'T foo(T, T) [with T = int]' after instantiation)
template <class T> T foo(T a, T b);
int main()
{
int x=34, y=54;
cout<<foo(x, y);
}
template <class T> T foo(T a, T b)
{
return a+b;
}
template <> int foo<int>(int a, int b)
{
cout<<"int specialization";
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个模板函数,它接受任何类型的T参数(现在是原语)并打印出其内容:
template<class T>
void displayContents(const vector<T>& data)
{
vector<T>::const_iterator i;
i=data.begin();
for( ; i!=data.end(); i++){
cout<<*i<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息是:
在函数'void displayContents(const std :: vector>&)'中:error:expected';' 在'我'之前 错误:'i'未在此范围内声明===构建完成:2个错误,0个警告===
我忽略了语法错误吗?
我是初学者,我一直在读一本关于C++的书,我正在写一个关于函数的章节.我写了一个来反转一个字符串,将它的副本返回给main并输出它.
string reverseInput(string input);
int main()
{
string input="Test string";
//cin>>input;
cout<<reverseInput(input);
return 0;
}
string reverseInput(string input)
{
string reverse=input;
int count=input.length();
for(int i=input.length(), j=0; i>=0; i--, j++){
reverse[j]=input[i-1];
}
return reverse;
}
Run Code Online (Sandbox Code Playgroud)
以上似乎都有效.更改以下代码时会发生此问题:
string input="Test string";
Run Code Online (Sandbox Code Playgroud)
至:
string input;
cin>>input;
Run Code Online (Sandbox Code Playgroud)
在此更改之后,反向函数仅返回第一个输入字的反转,而不是整个字符串.我无法弄清楚我哪里出错了.
最后,是否有更优雅的方法通过使用引用来做到这一点,而无需复制输入,以便输入变量本身被修改?
我有这样的结果集:
STAFF_NUM FLEET_CD EFF_DT
00046110 320 25-NOV-74 00:00
00046110 330 25-NOV-74 00:00
00046110 737 16-JAN-15 00:00
00046110 767 25-NOV-74 00:00
00046110 777 07-FEB-14 00:00
00046110 IL9 25-NOV-74 00:00
00046110 SU9 25-NOV-74 00:00
Run Code Online (Sandbox Code Playgroud)
是否有一个聚合函数,允许我将其分组/转换为单行,如下所示?
STAFF_NUM Fleets
00046110 320, 330, 737, 767, 777, IL9, SU9
Run Code Online (Sandbox Code Playgroud)