Basic use of C++

Basic use of C++

remove all spaces from a string

int begin = 0;
begin = s.find( " ", begin );
while (begin != -1)
{
    s.replace( begin, 1, "" );
    begin = s.find( " ", begin );
}

virtual

  • Reference link for detailed explanation: http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html

  • code

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <set>
    #include <string.h>
    #include <map>
    #include <unordered_map>
    #include <unordered_set>
    #include <iostream>
    #include <queue>
    #include <list>
    using namespace std;
    
    class Base
    {
    public:Base(){}
    public:
        virtual void print(){ cout << "Base" << endl; }
    
        void printWithoutVirtual() { cout << "Base without virtual" << endl; }
    };
    class Derived :public Base
    {
    public:Derived(){}
    public:
        void print(){ cout << "Derived" << endl; }
    
        void printWithoutVirtual() { cout << "Derived without virtual" << endl; }
    };
    int main()
    {
        Base *point = new Derived();
        point->print(); // 会输出Derived,即其实例化的对象的函数
        point->printWithoutVirtual(); // 输出Base without virtual,即其定义的类型
        system("pause");
        return 0;
    }
    

Constant pointers and pointer constants

  • Note: In fact, don't remember to be so confused, just remember: the content after const is constant, that is, the content after const will be locked and cannot be modified
  • Reference link: https://www.zhihu.com/question/19829354/answer/13091785

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <set>
    #include <string.h>
    #include <map>
    #include <unordered_map>
    #include <unordered_set>
    #include <iostream>
    #include <queue>
    #include <list>
    #include <assert.h>
    using namespace std;
    typedef long long ll;
    
    int main()
    {
        int a = 1;
        int aa = 2;
    
        const int b = 2;
        // b = 3; // 这句话会报错,因为常量进行初始化之后就无法修改
    
        const int *p1 = &a;
        p1 = &b;
        // *p1 = 3; //这句话会报错,不能改变const pointer的内容
        int const *p2 = &a;
        p2 = &b;
        // *p2 = 3; // p2的含义与p1相同
    
        int *const p3 = &a;
        *p3 = 4;
        cout << a << endl;
        //p3 = &aa; // 这句话会报错,p3为常量,不能改变其地址,但是可以改变其内容
    
        system("pause");
    }
    

function template

  • You can specify that one or more parameter types are unknown. When calling, you can explicitly specify its parameter type, and the compiler can automatically infer its parameter type.
  • example1

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <set>
    #include <string.h>
    #include <map>
    #include <unordered_map>
    #include <unordered_set>
    #include <iostream>
    #include <queue>
    #include <list>
    #include <assert.h>
    using namespace std;
    typedef long long ll;
    
    template<typename T>
    T mymax(T a, T b)
    {
        return a >= b ? a : b;
    }
    
    int main()
    {
        double a = 0, b = 1.3;
        // 可以自己推断类型,也可以指定其传入的类型
        cout << mymax( a, b ) << endl;
        cout << mymax<int>(a, b) << endl;
        return 0;
    }
    
  • example2

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <set>
    #include <string.h>
    #include <map>
    #include <unordered_map>
    #include <unordered_set>
    #include <iostream>
    #include <queue>
    #include <list>
    #include <assert.h>
    using namespace std;
    typedef long long ll;
    
    template <typename iteratorType, typename elemType>
    iteratorType myfind(iteratorType first, iteratorType last, const elemType &val)
    {
        while (first != last)
        {
            if (*first == val)
                return first;
            first++;
        }
        return last;
    }
    
    int main()
    {
    
        int a[8] = {1,2,3,4,5,6,7,8};
        vector<int> avec(a, a + 6);
        list<int> alist(a, a + 7);
        vector<int>::iterator it = myfind(avec.begin(), avec.end(), 3);
        if (it != avec.end())
            cout << "find num in container : " << *it << endl;
        else
            cout << "can not find value" << endl;
    
        list<int>::iterator it2 = myfind(alist.begin(), alist.end(), 6);
        if (it2 != alist.end())
            cout << "find num in container : " << *it2 << endl;
        else
            cout << "can not find value" << endl;
    
        system("pause");
        return 0;
    }
    

find_if, remove_if, bind1st, bind2nd, etc. use

#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <set>
#include <string.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <queue>
#include <list>
#include <assert.h>
#include <time.h>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;

int main()
{
    // 按照时间来初始化种子,保证每次结果不一样
    srand( (int)time(0) );
    //cout << rand() << endl;

    vector<int> nums = {1,8,3,5,7,4,9};

    vector<int> tmp( nums );
    // 找到第一个等于5的元素对应的it
    auto it = find_if( tmp.begin(), tmp.end(), bind2nd(equal_to<int>(), 5) );
    cout << *it << endl;

    // 删除容器中所有大于6的元素
    tmp.erase(remove_if(tmp.begin(), tmp.end(), bind1st(less<int>(), 6)), tmp.end());

    // 删除容器中所有小于6的元素
    tmp = nums;
    tmp.erase(remove_if(tmp.begin(), tmp.end(), bind2nd(less<int>(), 6)), tmp.end());

    // 删除所有相邻的相等元素
    nums = {1,2,2,2,3,5,3,6};
    tmp = nums;
    auto it1 = unique( tmp.begin(), tmp.end() );
    tmp.erase(it1, tmp.end());

    tmp = nums;
    vector<int> dest( nums.size() );
    it1 = unique_copy(tmp.begin(), tmp.end(), dest.begin());
    // 顺序型容器的iterator是可以与常数进行加减操作的
    cout << it1 - dest.begin() << endl;// 操作后的列表长度
    cout << *(it1 - 3) << endl; // 取出其中的元素
    tmp.resize(it1 - dest.begin());

    list<int> alist = {1,2,3,4};
    auto lit = alist.begin();
    // 如果使用lit+1,则会出现错误,因为list为关联型容器
    cout << *(++lit) << endl;


    system("pause");
    return 0;
}

Use of iostream_iterator

  • The input or output of the array can be realized in a very convenient form, and it also supports input and output from different ways.
  • code

    #include <stdio.h>
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <set>
    #include <string.h>
    #include <map>
    #include <unordered_map>
    #include <unordered_set>
    #include <iostream>
    #include <queue>
    #include <list>
    #include <assert.h>
    #include <time.h>
    #include <algorithm>
    #include <functional>
    #include <iterator>
    #include <fstream>
    using namespace std;
    typedef long long ll;
    
    // iostream"_iterator的用法
    int main()
    {
        // 按照时间来初始化种子,保证每次结果不一样
        srand( (int)time(0) );
        //cout << rand() << endl;
    
        string s;
        vector<string> strs1;
        // 常用的输入一个字符串列表的方法
        //while (cin >> s)
        //  strs1.push_back( s );
        //std::sort( strs1.begin(), strs1.end() );
    
        // 也可以使用istream_iterator实现,eof不指定具体对象时,表示输入到结束为止
        vector<string> strs2;
        istream_iterator<string> is( cin );
        istream_iterator<string> eof;
        copy( is, eof, back_inserter(strs2) );
    
        ostream_iterator<string> os( cout, "\n" );
        copy( strs2.begin(), strs2.end(), os );
    
        // 也可以将结果写入文件
        string fn = "result.txt";
        ofstream ofile(fn, ios::app);
        ostream_iterator<string> ofs( ofile, "\n" );
        copy(strs2.begin(), strs2.end(), ofs);
        ofile.close();
        vector<int> nums = {1,8,3,5,7,4,9};
    
        system("pause");
        return 0;
    }
    

wordCount on text files with filter effect

#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <string>
#include <set>
#include <string.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <queue>
#include <list>
#include <assert.h>
#include <time.h>
#include <algorithm>
#include <functional>
#include <iterator>
#include <fstream>
using namespace std;
typedef long long ll;

// 实现文本wordcount的功能,同时可以去除一些常用的词汇(a, the等)
int main()
{
    // 按照时间来初始化种子,保证每次结果不一样
    srand( (int)time(0) );
    //cout << rand() << endl;

    string inputFn = "words.txt";
    unordered_set<string> excluded = {"a", "the", "or", "and", "is"};
    unordered_map<string, int> wordsMap;
    ifstream is(inputFn);
    if (!is)
    {
        cerr << "read file failed!" << endl;
        return -1;
    }
    string s;
    while (is >> s)
    {
        if (excluded.count(s))
            continue;
        wordsMap[s]++;
    }
    for (auto it = wordsMap.begin(); it != wordsMap.end(); it++)
        cout << it->first << ", " << it->second << endl;


    system("pause");
    return 0;
}

inline

  • The inline function solves the problem of consuming stack memory due to calling a large number of smaller functions
  • The inline keyword is only useful when used in front of the function definition, but it does not work if it is placed in front of the function declaration. For details, see the reference link: http://www.cnblogs.com/berry/articles/1582702.html
  • The compiler will automatically convert the functions defined in the class to inline functions
  • The inline function is at the cost of code bloat, reducing the overhead of function calls (at each inline function, it will be copied and unrolled), so excessive use of inline functions will consume more memory space

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325486085&siteId=291194637