美文网首页
备份一些C++程序

备份一些C++程序

作者: 臻甄 | 来源:发表于2022-01-12 15:54 被阅读0次

平时Python写多了,突然让写C++还得适应和回忆一下

hello world

#include <iostream>

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World, This is abc!\n";
    return 0;
}

Hello, World, This is abc!

指针

#include <iostream>

void change(int* key) {
    *key = 5;
}
int main () {
    int key = 0;
    change(&key);
    std::cout << key << std::endl;
}

5

头文件写法

//
//  my_test.hpp
//  helloworld
//
//  Created by Li on 2021/1/7.
//  Copyright © 2021 Li . All rights reserved.
//

#ifndef my_test_hpp
#define my_test_hpp

#include <stdio.h>

#endif /* my_test_hpp */

#include "my_test.hpp"

std::tuple 将多个不同类型的成员捆绑成单一对象
std::array 是在C++11中才引入的,与内置数组相比,是一种更安全、更易用的数组类型

#include <iostream>
#include <array>

int main()
{
    typedef std::tuple<int, int, int, std::string, std::string> DATA_INFO;
    //initialization value
    const DATA_INFO info(28, 28, 10, "data", "prob");

    // get values:
    std::cout << "(" << std::get<0>(info) << ", " << std::get<1>(info)
              << ", " << std::get<2>(info) << ", " << std::get<3>(info)
              << ", " << std::get<4>(info) << ")\n";


    std::array<int, 3> arr;

    // set values:
    std::get<0>(arr) = 1;
    std::get<1>(arr) = 2;
    std::get<2>(arr) = 3;

    // get values:
    std::cout << "(" << std::get<0>(arr) << ", " << std::get<1>(arr)
              << ", " << std::get<2>(arr) << ")\n";
}

(28, 28, 10, data, prob)
(1, 2, 3)

大小写转换

#include <iostream>
int main(){
    std::string opt_type = "Sdg";
    std::transform(opt_type.begin(), opt_type.end(), opt_type.begin(), ::tolower);
    std::cout << "opt_type: " << opt_type << std::endl;
}

opt_type: sdg

vector

#include <iostream>
#include <iomanip>
#include <vector>

void print_arr(std::vector<const std::string>& arr) {
    std::cout << "length: " << arr.size() << std::endl;
    for (int i = 0; i < arr.size(); i++) {
        std::cout << arr[i] << ' ';
    }
    std::cout << std::endl;
}

int main(int argc, const char * argv[]) {
    std::cout << std::fixed << std::setprecision(10) << 1.23456 << " | " << 34 << std::endl;

    std::cout << "-------------- begin" << std::endl;
    std::vector<const std::string> arr1 = {"nnn1", "mmm1", "ooo1"};
    std::vector<const std::string> arr2 = {"nnn2", "mmm2", "ooo2"};

    std::vector<std::vector<const std::string> > arrs;
    arrs.push_back(arr1);
    arrs.push_back(arr2);
    arrs.erase(arrs.begin(), arrs.end()); // delete
    print_arr(arrs[0]);
    print_arr(arrs[1]);
    print_arr(arr1);
    print_arr(arr2);
    
    bool success = true;
    success = success && true;
    std::cout << success << std::endl;
    return 0;
}

1.2345600000 | 34
-------------- begin
length: 0

length: 0

length: 3
nnn1 mmm1 ooo1
length: 3
nnn2 mmm2 ooo2
1

map

#include <iostream>
#include <map>

int main(){
    // 定义一个map对象
    std::map<int, std::string> mapStudent;

    // 第一种 用insert函數插入pair
    mapStudent.insert(std::pair<int, std::string>(000, "student_zero"));

    // 第二种 用insert函数插入value_type数据
    mapStudent.insert(std::map<int, std::string>::value_type(001, "student_one"));

    // 第三种 用"array"方式插入
    mapStudent[123] = "student_first";
    mapStudent[456] = "student_second";

    for(std::map<int, std::string>::iterator iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {
        std::cout << iter->first << " : " << iter->second << std::endl;
    }
}

0 : student_zero
1 : student_one

123 : student_first
456 : student_second

创建数组,使用abs

#include <iostream>
#include <cmath>

int main() {
    std::cout << "abs" << std::abs(-0.2) << std::endl;
    int arr[4][4] =  { {1,  2,  3,   4},
                        {5,  6,  7,   8},
                        {9,  10, 11, 12},
                        {13, 14, 15, 16} } ;

    for (int i = 0; i < 2; ++i){
        for (int j = 0; j < 2; ++j) {
            std::cout << i << " " << j  << "  "<< arr[i][j] << std::endl;
        }
    }
}

abs0.2
0 0 1
0 1 2
1 0 5
1 1 6

生成随机数

#include <vector>
#include <iostream>
#include <random>
#include <ctime>

std::vector<unsigned> randomGenerate(const unsigned low, const unsigned high, const int len=10)
{
    srand(0);
    int key = rand();
//    int key = NULL;
//    int key = int(time(0));
    std::cout << "key " << key << std::endl;
    static std::default_random_engine generator(key);
    static std::uniform_int_distribution<unsigned> uniform(low, high); // get random int : uniform(generator)
//    static std::normal_distribution<float> norm; // get random float : norm(generator)

    std::vector<unsigned> vec;
    for (int i = 0; i < len; i++)
        vec.push_back(uniform(generator));
    return vec;
}

int main()
{
    for (int i = 0; i < 2; i++) {
        std::vector<unsigned> vec = randomGenerate(0, 30);
        for (auto &i : vec)
            std::cout << i << " ";
        std::cout << std::endl;
    }
    return 0;
}

key 520932930
26 13 1 3 0 5 25 12 4 20
key 520932930
3 28 17 1 20 20 28 24 19 6

  • new 可以调用对象的构造函数,对应的 delete 调用相应的析构函数
    (1)new创建对象时,先为对象分配内存空间,然后调用构造函数;
    (2)delete 删除对象时,先调用析构函数,然后回收内存空间
  • malloc 仅仅分配内存,free 仅仅回收内存,并不执行构造和析构函数
#include <iostream>

int main () {
    const int size = 4;
    const int src_size = 6;
    float src[src_size] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
    float* noise = new float [size];
    memset(noise, 0, size * sizeof(float));

    memcpy(noise, src, (size-1) * sizeof(float));

    for (int i = 0; i < src_size; ++i) {
        std::cout << i << ": " << src[i] << std::endl;
    }
    std::cout << "========"<< std::endl;
    for (int i = 0; i < size+7; ++i) {
        std::cout << i << ": " <<  noise[i] << std::endl;
    }
    std::cout << "========" << std::endl;
    std::cout << "sizeof(noise) " << sizeof(noise) << std::endl;
    std::cout << "sizeof(*noise) " << sizeof(*noise) << std::endl;
    std::cout << "sizeof(noise[0]) " << sizeof(noise[0]) << std::endl;
    std::cout << "sizeof(float) " << sizeof(float) << std::endl;
    std::cout << "size " << size << std::endl;
    std::cout << std::endl;
    delete[] noise;
}

========
sizeof(noise) 8
sizeof(*noise) 4
sizeof(noise[0]) 4
sizeof(float) 4
size 4

相关文章

网友评论

      本文标题:备份一些C++程序

      本文链接:https://www.haomeiwen.com/subject/ejywcrtx.html