![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++
fancy_male
这个作者很懒,什么都没留下…
展开
-
C++新特性
来自leetcode一条评论:二叉树的层平均值/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullpt原创 2021-10-06 17:23:08 · 84 阅读 · 0 评论 -
C++——迭代器iterator
反向迭代器,错误书写方式,这种方式好像会出错,不知道为什么://vector<ElemType> v;for (vector<int>::iterator j = v.end(); j != v.begin(); --j){}反向迭代器,正确书写方式://vector<ElemType> v;for (vector<int>::reverse_iterator j = v.rbegin(); j != v.rend(); ++j){}...原创 2021-10-03 00:26:06 · 44 阅读 · 0 评论 -
C++ stringstream
#include<bits/stdc++.h>using namespace std;int main(){ string s = "ab 12 av";//“ab12 av”结果一致 //“12av”输出12 stringstream ss; ss <<s; int i; ss >>i; cout<<i<<endl; return 0;}运行结果:最近赶作业,至于为什么以后再补。...原创 2020-12-17 00:08:52 · 4850 阅读 · 1 评论 -
C++ priority_queue为例的比较函数
很多时候,我们需要重载诸如priority_queue中的比较函数。在这其中有一些小细节,如下:1、经典错误及解决方案1.1错误写法struct node{public: node() { a = 1; } bool operator < (node b) { return a < b.a; } int a;};这样的比较函数在priority_queue容器中是无法通过的。如图,转载自某某同学:可以看到,里面用了const修饰。(MSVC的实现)1原创 2020-12-02 00:48:21 · 608 阅读 · 0 评论 -
C++ map []返回引用
Dev C++int main(){ map<char, int> m; m['a'] = 1; cout<<m['a']<<endl; m['a']++; cout<<m['a']<<endl; return 0;}可知map对[]运算符重载返回值为引用。原创 2020-12-01 21:49:28 · 1725 阅读 · 3 评论 -
C++ 构造函数对this的注意事项
VS2019报错错误代码:node::node(char _c, int n, node* _left, node* _right): this->c(_c), num(n), left(_left), right(_right){}正确代码:node::node(char _c, int n, node* _left, node* _right): c(_c), num(n), left(_left), right(_right){}原因:this指针属于对象,初始化列表在构原创 2020-12-01 20:28:58 · 519 阅读 · 0 评论 -
C++友元类声明
class A{public: A () { a = 1; } void print() { cout <<a<<endl; }// friend class B; 友元类也可放在此处 private: int a; friend class B;//这里不能为friend B; };class B{public: void f(A& _a) { _a.a = 2;//由于用到A的成员变量 //则必须先声明类A原创 2020-12-01 11:38:19 · 4009 阅读 · 0 评论 -
C++ ostringstream、istringstream
ostringstream:1、关于tellp()函数#include <iostream>#include <sstream>int main(){ std::ostringstream s; std::cout << s.tellp() << '\n'; s << 'h'; std::cout << s.tellp() << '\n'; s << "ello,原创 2020-11-17 20:31:21 · 238 阅读 · 0 评论 -
C++文件eof()函数的小细节
代码段:#include<bits/stdc++.h>using namespace std;int main(){ ifstream ifile("date.txt", ios::in); char s[100] = {0}; if (ifile) { int i = 0; for (char c ; !ifile.eof(); ++i) { ifile>>c; s[i] = c; } } cout<<s<<原创 2020-10-26 21:23:26 · 1943 阅读 · 2 评论 -
C++基类析构为虚函数是否一定会调用自类析构
#include<bits/stdc++.h>using namespace std;class A{public: A() { cout<< __FUNCTION__ <<endl; } virtual ~A() { cout<< __FUNCTION__ <<endl; }};class B : public A{public: B() { cout<< __FUNCTION__ &l原创 2020-10-25 20:39:30 · 131 阅读 · 0 评论