小编tan*_*ang的帖子

使用默认参数的C++函数模板

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <functional>
using namespace std;

template <typename Object, typename Comparator>
const Object &findMax(const vector<Object> &arr,
         const Comparator &isLessThan = less<Object>())
{
    int maxIndex = 0;

    for (int i = 1; i < arr.size(); i++) {
        if (isLessThan(arr[maxIndex], arr[i])) {
            maxIndex = i;
        }
    }
    return arr[maxIndex];
}

int main()
{
    vector<string> arr(3);
    arr[0] = "ZED";
    arr[1] = "alli";
    arr[2] = "crocode";
//...
    cout << findMax(arr) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我用g ++编译它时,它会出现以下错误: …

c++ templates

5
推荐指数
2
解决办法
4243
查看次数

内联函数如何在C++中工作?

现在我有2个c ++源文件:test9.cpp test10.cpp,它们都有一个同名的内联函数.

test9.cpp:

1 #include <iostream>
2 using namespace std;
3 void test();
4 inline void f1()
5 {
6     cout << "inline f1 in test9.cpp" << endl;
7 }   
8 int main()
9 {
10     f1();
11     test();
12     return 0;
13 } 
Run Code Online (Sandbox Code Playgroud)

test10.cpp:

1 #include <iostream>
2 using namespace std;
3 inline void f1()
4 {
5     cout << "inline f1 in test10.cpp" << endl;
6 }   
7 void test()
8 {
9     f1();
10 } 
Run Code Online (Sandbox Code Playgroud)

现在用g …

c++ inline

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

标签 统计

c++ ×2

inline ×1

templates ×1