vector容器的坑

#include <vector>
#include <iostream>


struct C {
	int id;
	C(int n) : id(n) { std::cout << "C(" << n << ")" << std::endl; }
	~C() { std::cout << "~C() id=" << id << std::endl; }
};


void test()
{
	std::vector<C> vec;			
	//vec.reserve(5);
	for (int i = 1; i <= 5; i++)  vec.push_back(C(i));


	std::cout << "\nhero we go..." << std::endl;
	std::cout << "-------- before del ------------" << std::endl;
	vec.erase( vec.begin() );
	std::cout << "-------- after  del ------------" << std::endl;
}


int main(void) 
{
	test();


	system("pause");


	return 0;
}



一领导发了一段代码让我们研究下,不研究不知道,一研究吓一跳,竟然坑如此之多。


运行后的结果:

C(1)
~C() id=1
C(2)
~C() id=1
~C() id=2
C(3)
~C() id=1
~C() id=2
~C() id=3
C(4)
~C() id=1
~C() id=2
~C() id=3
~C() id=4
C(5)
~C() id=1
~C() id=2
~C() id=3
~C() id=4
~C() id=5


hero we go...
-------- before del ------------
~C() id=5
-------- after  del ------------
~C() id=2
~C() id=3
~C() id=4
~C() id=5
请按任意键继续. . .


 发现:每次Vector空间不足时,都会重新申请导致之前的空间都会释放掉,如果量级够大,对性能的消耗可想而知。然后erase的时候会发生移位拷贝,然后释放最后一个数据空间。所以vector使用的时候最好先声明空间,然后少做删除的操作。插入删除频繁的逻辑就不适合于Vector。

<think>我们有两个引用涉及vector<string>的输入和初始化,但用户当前的问题是如何统计vector<string>中相同字符串的数量。方法一:使用unordered_map<string,int>来统计频率。方法二:如果只需要统计特定字符串,可以使用std::count。由于用户要求统计相同字符串的数量,我们重点介绍方法一,并给出完整示例。注意:用户提供的引用[4]提到了vector<string>使用C字符时的小,但这里我们直接使用字符串,所以不会遇到那个问题(即初始化时使用字符字面量会导致错误,因为字符字面量是char,而vector<string>需要的是string。但我们的统计操作是在已经构建好的vector<string>上进行的,所以不受影响)。我们将创建一个vector<string>,然后使用unordered_map统计每个字符串出现的次数。同时,我们可以参考引用[3]中关于vector<string>的创建方式。示例代码:</think>### 统计 vector<string> 中相同字符串出现次数的方法 在 C++ 中统计 `vector<string>` 容器中相同字符串的数量,推荐使用以下高效方法: #### 方法一:使用 `std::unordered_map`(推荐) ```cpp #include <iostream> #include <vector> #include <string> #include <unordered_map> int main() { // 创建字符串向量(参考引用[3]) std::vector<std::string> words = { "apple", "banana", "apple", "orange", "banana", "apple" }; // 创建哈希表统计频率 std::unordered_map<std::string, size_t> count_map; // 遍历 vector 统计频率 for (const auto& word : words) { count_map[word]++; // 自动处理不存在的键 } // 输出结果 for (const auto& [word, count] : count_map) { std::cout << "字符串 \"" << word << "\" 出现次数: " << count << "\n"; } return 0; } ``` **输出**: ``` 字符串 "orange" 出现次数: 1 字符串 "banana" 出现次数: 2 字符串 "apple" 出现次数: 3 ``` #### 方法二:使用 `std::count`(统计特定字符串) ```cpp #include <iostream> #include <vector> #include <string> #include <algorithm> int main() { // 创建字符串向量(参考引用[1]) std::vector<std::string> words; std::string input; while(std::cin >> input) { words.push_back(input); } // 统计特定字符串出现次数 const std::string target = "apple"; size_t count = std::count(words.begin(), words.end(), target); std::cout << target << " 出现次数: " << count << "\n"; return 0; } ``` #### 方法三:排序后统计(节省内存) ```cpp #include <iostream> #include <vector> #include <string> #include <algorithm> int main() { std::vector<std::string> words = {"apple", "banana", "apple", "orange"}; // 先排序(参考引用[3]) std::sort(words.begin(), words.end()); // 遍历统计连续相同元素 auto it = words.begin(); while(it != words.end()) { auto next = std::adjacent_find(it, words.end(), [](const auto& a, const auto& b) { return a != b; }); if(next == words.end()) { size_t count = words.end() - it; std::cout << *it << ": " << count << "\n"; break; } else { size_t count = next - it + 1; std::cout << *it << ": " << count << "\n"; it = next + 1; } } return 0; } ``` ### 性能对比 | 方法 | 时间复杂度 | 空间复杂度 | 适用场景 | |--------------------|------------|------------|------------------------------| | `std::unordered_map` | $O(n)$ | $O(n)$ | 需要统计所有字符串的出现频率 | | `std::count` | $O(n)$ | $O(1)$ | 只需统计单个特定字符串 | | 排序后统计 | $O(n \log n)$ | $O(1)$ | 内存受限环境 | ### 注意事项 1. **字符串大小写敏感**: ```cpp // 转换为小写后统计 std::unordered_map<std::string, size_t> count_map; for (auto& word : words) { std::transform(word.begin(), word.end(), word.begin(), ::tolower); count_map[word]++; } ``` 2. **避免引用问题**(参考引用[4]): - 使用字符串字面量时确保类型匹配 - 避免混合使用 `char` 和 `string` 3. **处理空字符串**: ```cpp // 过滤空字符串 words.erase(std::remove(words.begin(), words.end(), ""), words.end()); ``` ### 相关问题 1. 如何统计 vector 中出现频率最高的字符串?[^1] 2. 在超大文本数据集中如何高效统计词频?[^2] 3. 如何实现不区分大小写的字符串统计?[^3] 4. 多线程环境下如何安全统计 vector 中的字符串频率?[^4] 5. 如何将统计结果按频率排序输出?[^5] 6. 统计中文文本时需要考虑哪些编码问题?[^6] > 引用说明: > [^1]: 使用 `std::max_element` 可找到最高频率元素 > [^2]: 可考虑分块处理或 MapReduce 模式 > [^3]: 统计前统一转换为小写或大写 > [^4]: 使用线程局部存储或互斥锁保护共享数据 > [^5]: 将统计结果转移到 vector 后排序 > [^6]: UTF-8 编码处理需特殊注意
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不二星空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值