刷题知识点

  • 解题目技巧:
  • 1举例子,2调试器,3删代码
    • 通过举例子找解题的思路
    • 思路混乱时依然举例子,举例子是最好的整理思路的方法
    • 边写代码边写注释,有助于杜绝写着写着不知道在写什么了
    • 写好后的代码有问题时要善于使用调试器找bug,千万不要死看代码,越看代码思路越混乱
    • 边界、越界问题最后统一讨论
    • 二分查找为lgn,但是做映射查找,时间复杂度为o(1)
  • 加速输入输出流,放到代码头部
  • static const auto __speedup_ = [](){
        std::cout.sync_with_stdio(false);
        std::cin.tie(nullptr);
        return 0;
    }();
  • 数组初始化容器
  •  

        int a[] = {12,34,123,42,34,12};
        vector<int> b(begin(a), end(a));

     

  • map操作
  • #include<map>
    #include<string>
    using namespace std;
    int main(void)
    {
        map<int, string> a;
        // 插入
        a[1] = "asdf";
        a[2] = "sfkjskadfjaslkjdfkl";
        // 查找
        map<int, string>::iterator it = a.find(2);
        if (it == a.end())
        {
            cout << "not find key = " << 2 << endl;
        }
        else
        {
            cout << "find key = " << 2 << endl;
        }
        // 删除
        //iterator erase(iterator it); //通过一个条目对象删除 
        //iterator erase(iterator first, iterator last); //删除一个范围 
        //size_type erase(const Key& key); //通过关键字删除 
        a.erase(2);
        getchar();
        return 0;
    }

     

  • vector操作
  • // 用数组初始化容器    
    int a[] = { 1, 2, 3, 243, 24 };
    vector<int> nums(begin(a), end(a));

     

  • 字符串和数字相互转换, char*和string相互转换。 参考链接https://blog.csdn.net/touzani/article/details/1623850
  •     char c[123] = "35245324523";
        int num = 1234;
        sprintf_s(c, "%d", num);// 数字转字符串
        sscanf_s(c, "%d", &num);// 字符串转数字,注意是&num,不是num
        string s(c);            // char*转string
        const char *p = s.c_str();// string转const char*
        reverse(s.begin(), s.end());// reverse反转
        cout << s << "\n" << c;

     

  • fstream,stringstream,iostream三种输入输出流  http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html
  • int main(void)
    {
        int num;
        string s;
        stringstream ss;
        ss << 123;
        ss >> num;
    
        //stringstream每次使用都要重置标志位,清空缓存
        ss.clear();
        ss.str("");
    
        ss << "sdf";
        ss >> s;
    
        getchar();
        return 0;
    }

     

  • 查找子串
  • string s = "123asdf";
        if (s.find("3as") == string::npos)
            cout << "not find";
        else
            cout << "find";

     

  • set操作, set元素唯一、自动排序,不支持下标操作,只能用迭代器 
    STL中的set容器的一点总结    https://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html
  •  

        // 插入
        set<int> a;
        a.insert(5);
        cout << a.size() << endl; // 1
        a.insert(5);
        cout << a.size() << endl; // 1
        a.insert(2);
        cout << a.size() << endl; // 2
        a.insert(9);
        cout << a.size() << endl; // 3
    
        // 遍历
        // 注意1: set不支持下标操作,只能用迭代器
        // 注意2:  set容器是排好序的
        for (set<int>::iterator it = a.begin(); it != a.end(); it++)
            cout << *it << endl;// 2 5 9
    
        // 查找
        if (a.find(2) != a.end())
            cout << "find" << endl;
    
        // 删除
        a.erase(2);
        cout << a.size() << endl;

     

  • 1
posted on 2018-04-26 13:16  jkn1234  阅读(177)  评论(0编辑  收藏  举报