C++基本使用

C++基本使用

去除字符串中的所有空格

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

virtual

  • 详细解释的参考链接:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html

  • 代码

    #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;
    }
    

常量指针与指针常量

  • 注意:其实不用记得这么混淆,只需要记住:const后面的内容为常量即可,即const后面的内容会被锁定而无法修改
  • 参考链接: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");
    }
    

函数模板

  • 可以指定一个或者多个参数类型未知。在调用时,可以显示指定其参数类型,编译器也可以自动推断出其参数类型
  • 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等使用

#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;
}

iostream_iterator的使用

  • 可以以很方便的形式实现数组的输入或者输出,也支持从不同的途径进行输入与输出。
  • 代码

    #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

#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

  • inline函数解决因为调用大量比较小的函数而消耗栈内存的问题
  • inline关键字用于函数定义的前面才有作用,只是放在函数声明前面是不起作用的,详见参考链接:http://www.cnblogs.com/berry/articles/1582702.html
  • 编译器会将定义在类内的函数自动转换为inline函数
  • inline函数是以代码膨胀为代价,减小函数调用的开销(在每个inline函数处,都会对其进行复制展开),因此过度使用inline函数会消耗更多的内存空间

猜你喜欢

转载自blog.csdn.net/u012526003/article/details/80065661
今日推荐