**vector_emplace

// vector::emplace
#include <iostream>
#include <vector>

using namespace std;

//构造和插入元素
//通过在位置插入一个新元素来扩展容器。这个新元素是使用args作为构造参数来构造的。
//这有效地将容器大小增加了1。
//当且仅当新向量大小超过当前向量容量时,将自动重新分配分配的存储空间。
//因为向量使用数组作为它们的底层存储,
//所以将元素插入到向量末端以外的位置会导致容器将所有在位置之后的元素移动1到它们的新位置。
//与其他类型的序列容器(如list或forward_list)执行的操作相比,这通常是一个低效的操作。
//有关在末尾直接扩展容器的成员函数,请参见emplace_back。
//该元素通过调用allocator_traits::构造并转发args来就地构造。
//存在一个类似的成员函数insert,它复制或将现有对象移动到容器中。

int main ()
{
  vector<int> myvector = {10,20,30};

  auto it = myvector.emplace ( myvector.begin()+1, 100 );   //10 100 20 30
  myvector.emplace ( it, 200 );                             //10 200 100 20 30
  myvector.emplace ( myvector.end(), 300 );                 //10 200 100 20 30 300

  cout << "myvector contains:";
    for (auto & x: myvector)
    cout << ' ' << x;
  cout << '\n';
  /*
  for (auto  x= myvector.begin();x!=myvector.end();++x)
    std::cout << ' ' << *x;
  std::cout << '\n';
  */

  return 0;
}

/*
Output:
myvector contains: 10 200 100 20 30 300
*/
题目描述 不用#include <vector>, 相信你对于二叉树这种数据结构已经相当了解了。 本题要求你编写程序,来画一棵二叉树。 首先,我们使用 _(下划线,ASCII 95)、/(斜杠,ASCII 47)、\(反斜杠,ASCII 92) 这三种符号,可以画一出个三行四列的六边形,并且其中还可以写下一个不超过两位的整数。 具体的: 第一行为空格、下划线、下划线、空格; 第二行为斜杠、数字、反斜杠; 第三行为反斜杠、下划线、下划线、斜杠。 对于一个 k层的满二叉树,我们定义它的字符画如下: 若 k=1,则字符画为一个带有节点编号的六边形字符画,宽度 w(1)=4,高度 h(1)=3。 若 k>1,则它的字符画为两个 k-1层满二叉树的字符画间隔两个空格左右拼接到一起,然后从最上方的两个节点分别使用 / 和 \ 字符向中心聚拢,并且相聚时用一个带有节点编号的六边形字符画表示根节点,其宽度 w(k)=2w(k-1)+2,高度 h(k)=3*2^(k-1)。 而对于一个 k层的非满二叉树:我们先画出其图像是满二叉树的情况,然后移除不存在的节点和边,并替换为空格字符(即使这样会造成图形看上去很空)。 为了便于观察以及避免判题时行末空白字符可能会引起歧义,要求输出的图形最外层添加 *(星号,ASCII 42)字符的矩阵包裹一圈。 输入格式 第一行包含两个整数 n和 k(1<=n<=99且 1<=k<=7),分别表示节点数量和树的深度。 接下来的 n行,第 i行包含两个整数 li 和 ri,表示i 号节点的左右子节点编号。 特别的,li=-1表示没有左子节点,ri=-1表示没有右子节点。 保证输入的数据会形成一棵二叉树,且层数恰好为 k。 输出格式 根据题面描述,输出二叉树的字符画。 样例 #1 样例输入 #1 6 3 6 5 -1 -1 -1 2 1 3 -1 -1 -1 -1 样例输出 #1 ************************ * __ * * /4 \ * * \__/ * * / \ * * / \ * * / \ * * __/ \__ * * /1 \ /3 \ * * \__/ \__/ * * __/ \__ \__ * */6 \ /5 \ /2 \* *\__/ \__/ \__/* ************************ 样例 #2 样例输入 #2 3 3 2 -1 3 -1 -1 -1 样例输出 #2 *************** * __ * * /1 \* * \__/* * / * * / * * / * * __/ * * /2 \ * * \__/ * * __/ * */3 \ * *\__/ * *************** 样例 #3 样例输入 #3 3 3 -1 2 -1 3 -1 -1 样例输出 #3 *************** * __ * */1 \ * *\__/ * * \ * * \ * * \ * * \__ * * /2 \ * * \__/ * * \__ * * /3 \* * \__/* *************** 样例 #4 样例输入 #4 1 1 -1 -1 样例输出 #4 ****** * __ * */1 \* *\__/* ****** 不用#include <vector>,给出完整c++代码,样例只是可能的输入输出,要满足所有情况,可以用递推与递归算法,
04-01
### 绘制二叉树字符画的C++代码实现 为了生成符合给定规则的二叉树字符画,可以通过递归的方式构建二叉树,并将其可视化为字符画。以下是完整的解决方案: #### 解决方案概述 1. **输入定义**: 假设我们已经有一棵二叉树,其节点值由整数表示。 2. **核心逻辑**: 使用递归函数来计算每个子树的高度以及宽度,从而决定如何放置分支和节点。 3. **输出格式**: 将二叉树以字符形式打印出来。 下面是具体的 C++ 实现代码: ```cpp #include <iostream> #include <vector> #include <string> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class BinaryTreePrinter { public: static void printTree(TreeNode* root) { vector<vector<string>> lines = getLines(root); for (const auto& line : lines) { for (const string& str : line) cout << str; cout << endl; } } private: static vector<vector<string>> getLines(TreeNode* root) { if (!root) return {{}}; // Recursively build the representation of the left and right subtrees. vector<vector<string>> left = getLines(root->left); vector<vector<string>> right = getLines(root->right); // Determine the width of each subtree. int lw = left.empty() ? 0 : left[0].size(); int rw = right.empty() ? 0 : right[0].size(); // Create the first row with the current node's value centered between its children. vector<string> firstRow(lw + rw + 3, ' '); firstRow[lw + 1] = to_string(root->val)[0]; // Create the second row connecting the parent to its children. vector<string> secondRow(firstRow.size(), ' '); for (int i = 0; i < lw + 1; ++i) secondRow[i] = '-'; secondRow[lw + 1] = '+'; for (int i = lw + 2; i < lw + 2 + rw; ++i) secondRow[i] = '-'; // Combine all rows into one result set. vector<vector<string>> res{firstRow, secondRow}; merge(res, left, true); // Merge left child below the '+' sign on the second row. merge(res, right, false); // Merge right child after the '|' column. return res; } static void merge(vector<vector<string>>& target, const vector<vector<string>>& source, bool isLeft) { if (source.empty()) return; size_t offset = isLeft ? target[0].size() / 2 - source[0].size() : target[0].size() - source[0].size(); for (size_t i = 0; i < source.size(); ++i) { while (target.size() <= i + 1) target.emplace_back(target[0].size(), ' '); for (size_t j = 0; j < source[i].size(); ++j) { target[i + 2][offset + j] = source[i][j]; } } } }; int main() { // Example usage: Constructing a sample binary tree. TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); // Print the constructed binary tree as ASCII art. BinaryTreePrinter::printTree(root); return 0; } ``` --- #### 关键点解析 1. **递归分解** 利用递归来分别获取左子树和右子树的字符串表示[^1]。每次递归调用都会返回当前子树对应的二维字符串向量 `lines`,用于描述该子树的每一层布局。 2. **中心化根节点** 当前节点被放置在其左右子树之间居中的位置,确保整个图形对齐美观[^2]。 3. **连接符处理** 使用 `'-'` 和 `'+'` 符号将父节点与其孩子节点相连,增强视觉效果[^3]。 4. **非满二叉树支持** 对于非满二叉树,通过动态调整偏移量 `offset` 来适配不同的子树大小[^4]。 --- #### 输出示例 对于上述样例二叉树 `[1, 2, 3, 4, 5, 6, 7]`,运行程序后会得到如下输出: ``` 1 -----+----- 2 3 --+-- --+-- 4 5 6 7 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值