c++可变长度对象(vector,map)作为struct成员

以前,C语言写结构体时。结构体成员都是基本类型,比如int、char、double之类的,最多也就里边再套一个结构体成员,但它们都有一个特点。它们的大小是固定的,我不会有这样的担心。但是,我接触到string、vector这些类类型时,根据它们的特性:它们能动态扩充,就是你来多少内容,我都能存的下!

       然后,我就想:那它们的变量大小是不是也扩充了,那是不是也导致所处的结构体的变量的大小也改变了呢?

 

       因此,我查了下书。找到了答案:总结一下,简单来说就是,这些动态可变更存储长度的类都是使用指针,附加动态申请内存实现的而动态申请的内存本身并不包含在对象的本身大小上面,在对象中保存一个指针,它能够找到动态申请的内存的地址。所以,你不管动态申请多少内存,它对象中的一个指针总能找得到你,它指针的大小不会发生改变。也就是说,它所在的结构体变量的大小也是不会改变的!

      我也写了一个小程序检查了一下,大家可以通过运行结果看到结构体变量的大小并没有改变!

#include <iostream>
#include <string>
#include <vector>
 
using namespace std;
 
typedef struct Vec
{
	vector<int> int_vec;
}Int_vec;
 
typedef struct Str
{
	string str;
}Str_string;
 
int main(void)
{
 
	Int_vec myInt;
	Str_string myString;
 
	cout << "空的 myInt 大小:" << sizeof(myInt) << endl << endl;
	cout << "空的 myString 大小:" << sizeof(myString) << endl << endl;
	
	
	for (int i = 0; i < 10;i++)
	{
		myInt.int_vec.push_back(i);
	}
 
	myString.str = "student";
	cout << "有十个元素的 myInt 大小:" << sizeof(myInt) << endl << endl;
	cout << "有字符串内容的 myString 大小:" << sizeof(myString) << endl << endl;
 
	return 0;
}

原文:https://www.freesion.com/article/4349729055/

哈夫曼编码是一种可变字长编码(VLC)的一种方法,主要用于数据压缩。C++可以通过以下步骤实现哈夫曼编码: 1. 构建哈夫曼树 2. 根据哈夫曼树生成哈夫曼编码表 3. 使用哈夫曼编码表对原始数据进行编码 以下是一个简单的C++实现示例: ``` #include <iostream> #include <queue> #include <vector> #include <unordered_map> using namespace std; // 定义哈夫曼树节点 struct TreeNode { char ch; int freq; TreeNode *left; TreeNode *right; TreeNode(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {} ~TreeNode() { if (left != nullptr) delete left; if (right != nullptr) delete right; } }; // 定义比较函数 struct Compare { bool operator()(const TreeNode *lhs, const TreeNode *rhs) const { return lhs->freq > rhs->freq; } }; // 构建哈夫曼树 TreeNode *buildHuffmanTree(const unordered_map<char, int> &freqMap) { priority_queue<TreeNode *, vector<TreeNode *>, Compare> pq; for (const auto &[ch, freq] : freqMap) { pq.push(new TreeNode(ch, freq)); } while (pq.size() > 1) { auto left = pq.top(); pq.pop(); auto right = pq.top(); pq.pop(); auto parent = new TreeNode('\0', left->freq + right->freq); parent->left = left; parent->right = right; pq.push(parent); } auto root = pq.top(); pq.pop(); return root; } // 生成哈夫曼编码表 void generateHuffmanCode(const TreeNode *root, string code, unordered_map<char, string> &codeMap) { if (root == nullptr) return; if (root->left == nullptr && root->right == nullptr) { codeMap[root->ch] = code; return; } generateHuffmanCode(root->left, code + "0", codeMap); generateHuffmanCode(root->right, code + "1", codeMap); } // 对原始数据进行编码 string encode(const string &input, const unordered_map<char, string> &codeMap) { string output; for (const auto &c : input) { output += codeMap.at(c); } return output; } int main() { // 统计字符出现频率 string input = "hello world"; unordered_map<char, int> freqMap; for (const auto &c : input) { freqMap[c]++; } // 构建哈夫曼树 auto root = buildHuffmanTree(freqMap); // 生成哈夫曼编码表 unordered_map<char, string> codeMap; generateHuffmanCode(root, "", codeMap); // 对原始数据进行编码 string encoded = encode(input, codeMap); // 输出结果 cout << "Encoded: " << encoded << endl; for (const auto &[ch, code] : codeMap) { cout << ch << ": " << code << endl; } // 释放内存 delete root; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值