2021/10/09  阅读：47  主题：自定义主题1

# 活用三目运算符

``  ...  const string s = "1234567";  ...  foo(s); // foo(const string&)  bar(s); // bar(const string&)  ...``

``  string s = "1234567";  if (v.size() > 0) {      s = v[0];  }  ...  foo(s);  bar(s);  ...``

``  string s;  if (v.size() > 0) {      s = v[0];  } else {      s = "1234567";  }  ...  foo(s);  bar(s);  ...``

``  const string def_s = "123467";  const string* sp = &def_s;  if (v.size() > 0) {      sp = &v[0];  }  const auto& s = *sp;  ...  foo(s);  bar(s);  ...``

``  const auto& s = v.size() > 0 ? v[0]: "1234567";    ...  foo(s);  bar(s);  ...``

# 通过leetcode题目来看swap

leetcode 102. 二叉树的层序遍历:

``class Solution {public:   vector<vector<int>> levelOrder(TreeNode* root) {   }};``

``class Solution {public:    vector<vector<int>> levelOrder(TreeNode* root) {        if (!root) {            return {};        }        vector<vector<int>> ans;        queue<TreeNode*>* q = new queue<TreeNode*>;        q->push(root);        while (!q->empty()) {            queue<TreeNode*>* tmp_q = new queue<TreeNode*>;            vector<int> v;            while (!q->empty()) {                                TreeNode* node = q->front();                if (node) {                    v.push_back(node->val);                    tmp_q->push(node->left);                    tmp_q->push(node->right);                }                q->pop();            }              // 调整q的指向            q = tmp_q;            if (!v.empty()) {                ans.push_back(move(v));            }        }        return ans;    }};``

``class Solution {public:    vector<vector<int>> levelOrder(TreeNode* root) {        if (!root) {            return {};        }        vector<vector<int>> ans;        queue<TreeNode*> q;        q.push(root);        while (!q.empty()) {            vector<TreeNode*> tmp_v;            vector<int> v;            while (!q.empty()) {                                TreeNode* node = q.front();                if (node) {                    v.push_back(node->val);                    tmp_v.push_back(node->left);                    tmp_v.push_back(node->right);                }                q.pop();            }            // 遍历，入队            for (TreeNode* node: tmp_v) {                q.push(node);            }            if (!v.empty()) {                ans.push_back(move(v));            }        }        return ans;    }};``

``class Solution {public:    vector<vector<int>> levelOrder(TreeNode* root) {        if (!root) {            return {};        }        vector<vector<int>> ans;        queue<TreeNode*> q;        q.push(root);        while (!q.empty()) {            queue<TreeNode*> tmp_q;            vector<int> v;            while (!q.empty()) {                                TreeNode* node = q.front();                if (node) {                    v.push_back(node->val);                    tmp_q.push(node->left);                    tmp_q.push(node->right);                }                q.pop();            }           // 交换q和tmp_q的内部存储            q.swap(tmp_q);            if (!v.empty()) {                ans.push_back(move(v));            }        }        return ans;    }};``

STL容器会占用栈存储和堆存储，比如vector，即使你使用的局部变量的vector，它内部也会把具体的数据存储用堆来存储，在类内部使用一个指针指向这块内存而已。

# 往期推荐

2021/10/09  阅读：47  主题：自定义主题1