C/C++编程:打印任何容器&&生成随机数组

1059 篇文章 285 订阅

打印任何容器

#include <iostream>
#include <list>
#include <vector>
#include <map>

template<typename T>
std::ostream& print(std::ostream &out,T const &val) {
    return (out << val << " ");
}

template<typename T1,typename T2>
std::ostream& print(std::ostream &out,std::pair<T1,T2> const &val) {
    return (out << "{" << val.first << " " << val.second << "} ");
}

template<template<typename,typename...> class TT,typename... Args>
std::ostream& operator<<(std::ostream &out,TT<Args...> const &cont) {
    for(auto&& elem : cont) print(out,elem);
    return out;
}
int main() {
    std::vector<int> vector = {5, 9, 6, 8, 9};
    std::cout << vector << "\n";



    std::list<int> list = {5, 9, 6, 8, 9};
    std::cout << list << "\n";


    std::map<int, int> map = {{1, 2}, {3, 4}};
    std::cout << map;

    return 0;
}



在这里插入图片描述

如何往一个vector数组中整体加入另一个vector数组

    vector<int> v1 {2233},v2 {2, 3, 6};

    v1.insert(v1.end(),v2.begin(),v2.end());

    std::cout << v1;

在这里插入图片描述

数组

打印数组

template<typename T>
void display(std::vector<T>& vec) {
    for (auto it: vec) {
        std::cout << it << ",";
    }
    std::cout << "\n";
}


int main() {
    std::vector<std::string> vec{"aaa", "bbb", "ccc"};
    display(vec);
    return 0;
}
 std::copy(arr.begin(), arr.end(), std::ostream_iterator<T>(std::cout, " "));
auto print = [](const auto & element){std::cout << element << " ";};
std::for_each(vec.begin(), vec.end(), print);

手动生成数组

template<typename T>
void inputVector(std::vector<T>& vec) {
    T data;
    std::cin >> data;
    vec.push_back(data);
    while(std::cin.get() != '\n') {
        std::cin >> data;
        vec.push_back(data);
    }
}


int main() {
    std::vector<std::string> arr;
    std::cout << "input: ";
    inputVector(arr);
    std::cout << "display: ";
    std::copy(arr.begin(), arr.end(), std::ostream_iterator<std::string>(std::cout, " "));
    return 0;
}

在这里插入图片描述

生成随机数组

#include <assert.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iterator>


// compare two numbers
template<typename T>
bool compare(T a, T b) {
    return a < b;
}

template<typename T>    // absolute right because we use STL
void rightMethod(std::vector<T>& vec){
    sort(vec.begin(), vec.end(), compare<T>);
//    sort(vec.begin(), vec.end(), std::less<T>()); // official
}


std::default_random_engine e;
std::vector<int>  generateRandom(int maxLen, int minValue, int maxValue){
    std::uniform_int_distribution<int> distS(1, maxLen);
    std::uniform_int_distribution<int> distV(minValue, maxValue);
    std::vector<int> arr;
    int size = distS(e);
    arr.resize(size);
    for (int i = 0; i < size; ++i) {
        arr[i] = distV(e);
    }

    return arr;
}

int main() {
    e.seed(time(NULL));
    int testCount(10);    // epoch
    int maxSize(10);    // max size of the vector
    int minValue(-100);  // min value
    int maxValue(100);     // maxvalue
    for (int i = 0; i < testCount; ++i) {
        std::vector<int> vec1 = generateRandom(maxSize,  minValue, maxValue);
        rightMethod(vec1);
        std::copy(vec1.begin(), vec1.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << "\n";
    }



    return 0;
}

两个数组是否相等

template<typename T>
bool isEqual(std::vector<T>& vec1, std::vector<T>& vec2) {
    if (vec1.size() != vec2.size()) {
        return false;
    } else {
        return equal(vec1.begin(), vec1.end(), vec2.begin());   // STL equal
    }
}



int main() {
    std::vector<int> arr1 {1, 2, 3, 4, 4};
    std::vector<int> arr2 {1, 2, 3, 4, 4};
    if(isEqual(arr1, arr2)){
        printf("equal ok\n");
    }else{
        printf("arr1, arr2 should equal , but not equal\n");
    }

    arr1 = {1, 2, 3, 4, 4};
    arr2 = {1, 2, 3, 4, 5};

    if(isEqual(arr1, arr2)){
        printf("arr1, arr2 should not equal , but equal\n");
    }else{
        printf("not equal ok\n");
    }
    std::vector<std::string> str1 {"1", "2", "3", "4", "4"};
    std::vector<std::string> str2 {"1", "2", "3", "4", "4"};

    if(isEqual(str1, str2)){
        printf("equal ok\n");
    }else{
        printf("str1, str2 should equal , but not equal\n");
    }

    str1 = {"1", "2", "3", "4", "4"};
    str2 = {"1", "2", "3", "4", "5"};
    if(isEqual(str1, str2)){
        printf("str1, str2 should not equal , but  equal\n");
    }else{
        printf("not equal ok\n");
    }
    return 0;
}

拷贝数组

template<typename T>
void copyVector(std::vector<T>& vec1, std::vector<T>& vec2) {
    if (vec2.empty()) { // copy the vec2 to the vec1
        return;
    }
    vec1 = vec2;
}


template<typename T>
bool isEqual(std::vector<T>& vec1, std::vector<T>& vec2) {
    if (vec1.size() != vec2.size()) {
        return false;
    } else {
        return equal(vec1.begin(), vec1.end(), vec2.begin());   // STL equal
    }
}




int main() {
    std::vector<int> arr1 {1, 2, 3, 4, 4};
    std::vector<int> arr2;

    copyVector(arr2, arr1);

    if(isEqual(arr1, arr2)){
        printf("equal ok\n");
    }else{
        printf("arr1, arr2 should equal , but not equal\n");
    }


    std::vector<std::string> str1 {"1", "2", "3", "4", "4"};
    std::vector<std::string> str2;
    copyVector(str2, str1);

    if(isEqual(str1, str2)){
        printf("equal ok\n");
    }else{
        printf("str1, str2 should equal , but not equal\n");
    }
    
    return 0;
}

清空数组

  std::vector<int> ().swap(vec3);

对数器

#include <assert.h>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdlib>
#include <ctime>



template<typename T> // here we use different functional pointer
void testAlgorithm(void (*sortFunc) (std::vector<T>&)) {
    int testCount(100000);    // epoch
    int maxSize(10);    // max size of the vector
    int minValue(-100);  // min value
    int maxValue(100);     // maxvalue
    bool successFlag(true); // flag
    std::clock_t startTime, endTime;

    startTime = std::clock();   // start clock
    for (int i = 0; i < testCount; ++i) {
        std::vector<int> vec1, vec2, vec3;
        generateRandomVector(vec1, maxSize, minValue,maxValue); // generate random vector
        copyVector(vec2, vec1);
        copyVector(vec3, vec1); // help to show
        rightMethod(vec1);  // absolute right algorithm
        sortFunc(vec2);   // we want to test the algorithm

        if (!isEqual(vec1, vec2)) {
            successFlag = false;    // something wrong
            display(vec3);
            break;
        }
        // clean the usage of the vector, std::vector<T> ().swap()
        std::vector<int> ().swap(vec1);
        std::vector<int> ().swap(vec2);
        std::vector<int> ().swap(vec3);

    }
    if (successFlag) {
        std::cout << "Nice!" << std::endl;
    } else {
        std::cout << "There may be something wrong!" << std::endl;
    }
    endTime = std::clock();
    std::cout << "Total usage is: " << (endTime - startTime) / double(CLOCKS_PER_SEC) << 's' << std::endl;
}
//std::vector<>
int main() {

    testAlgorithm(insertSort<int>); // insert sort

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值