简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
1.前言
本篇目的:理解C++之vector<vector>中的vector元素。
2.应用实例
1.遍历vector中的元素
#include <iostream>
#include <vector>
int main() {
std::vector<int> intVector = {1, 2, 3, 4, 5};
// 使用 for 循环遍历元素
for (const auto& num : intVector) {
std::cout << num << " ";
}
std::cout << std::endl;
// 使用索引方式遍历元素
for (size_t i = 0; i < intVector.size(); ++i) {
std::cout << intVector[i] << " ";
}
std::cout << std::endl;
// 使用迭代器方式遍历元素
for (auto it = intVector.begin(); it != intVector.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
2.访问vector<vector<char>>中的vector<char>元素
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<char>> charVector = {
{'H', 'e', 'l', 'l', 'o'},
{'W', 'o', 'r'},
{'0', '1', '2', '3', '4', '5'}
};
//打印
printf("charVector.size() = %ld\n",charVector.size());
printf("charVector[0].size() = %ld\n",charVector[0].size());
printf("charVector[0] = %s\n",charVector[0].data());
printf("\n");
printf("charVector[1].size() = %ld\n",charVector[1].size());
printf("charVector[1] = %s\n",charVector[1].data());
printf("\n");
printf("charVector[2].size() = %ld\n",charVector[2].size());
printf("charVector[2] = %s\n",charVector[2].data());
printf("\n");
//遍历
for(int i = 0; i<charVector.size(); i++){
printf("data[%d] = %s, len = %ld",i, charVector[i].data(),charVector[i].size());
printf("\n");
}
return 0;
}