c模板编程cc++20240401

admin2024-04-03  0

c++模板编程

#include<iostream>
//#include<string>
//#include<algorithm>
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    int i = max(1, 2); // 返回 2
    float f = max(3.14f, 2.72f); // 返回 3.14
    std::string s = std::max(std::string("apple"), std::string("banana")); // 返回 "banana"

    std::cout << i << std::endl;
    std::cout << f << std::endl;
    std::cout << s << std::endl;
}//main()

#include<iostream>
//#include<string>
//#include<algorithm>
#include<vector>
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

typedef unsigned long long Ull;

std::vector<Ull> vec = { 2,3,4 };


int main() {
    int i = max(1, 2); // 返回 2
    float f = max(3.14f, 2.72f); // 返回 3.14
    std::string s = std::max(std::string("apple"), std::string("banana")); // 返回 "banana"

    std::cout << i << std::endl;
    std::cout << f << std::endl;
    std::cout << s << std::endl;

    for (auto i : vec) {
		std::cout << i << std::endl;
	}//

    for (Ull i = 0; true; ++i) {//for220

        vec.emplace_back(i);
        if ( 0 ==i % 100000000) {
            Ull lastElement= vec.back();
            std::cout << lastElement << ", ";
        }
    }//for220


}//main()
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!