做app网站的软件有哪些,软件开发培训视频,杭州网站建设 seo,wordpress前台不显示图片cSTL容器的使用#xff08;vector, list, map, set等#xff09; 在C的STL#xff08;Standard Template Library#xff09;中#xff0c;容器是重要的一部分#xff0c;它们提供了各种数据结构来存储和管理数据。以下是一些常见的STL容器及其使用方法的简要说明#x…cSTL容器的使用vector, list, map, set等 在C的STLStandard Template Library中容器是重要的一部分它们提供了各种数据结构来存储和管理数据。以下是一些常见的STL容器及其使用方法的简要说明 
Vector向量 std::vector 是一个动态数组可以动态地添加和删除元素。 
#include vector  
#include iostream  int main() {  // 创建一个空的vector  std::vectorint vec;  // 向vector中添加元素  vec.push_back(1);  vec.push_back(2);  vec.push_back(3);  // 访问元素  std::cout  First element:   vec[0]  std::endl;  // 遍历vector  for (const auto elem : vec) {  std::cout  elem   ;  }  // 删除元素  vec.pop_back(); // 删除最后一个元素  return 0;  
}List列表 std::list 是一个双向链表提供了高效的插入和删除操作。 
#include list  
#include iostream  int main() {  // 创建一个空的list  std::listint lst;  // 向list中添加元素  lst.push_back(1);  lst.push_front(0);  // 遍历list  for (const auto elem : lst) {  std::cout  elem   ;  }  // 删除元素  lst.remove(1); // 删除所有值为1的元素  return 0;  
}Map映射 std::map 是一个关联容器它存储的元素是键值对并按键进行排序。 
#include map  
#include iostream  int main() {  // 创建一个空的map  std::mapstd::string, int m;  // 向map中添加元素  m[apple]  1;  m[banana]  2;  // 访问元素  std::cout  Apple count:   m[apple]  std::endl;  // 遍历map  for (const auto pair : m) {  std::cout  pair.first  :   pair.second  std::endl;  }  // 删除元素  m.erase(banana); // 删除键为banana的元素  return 0;  
}Set集合 std::set 是一个关联容器它存储的元素是唯一的并按值进行排序。 
#include set  
#include iostream  int main() {  // 创建一个空的set  std::setint s;  // 向set中添加元素  s.insert(1);  s.insert(2);  s.insert(1); // 这个元素不会被插入因为set中不允许重复  // 遍历set  for (const auto elem : s) {  std::cout  elem   ;  }  // 删除元素  s.erase(1); // 删除值为1的元素  return 0;  
}在使用这些STL容器时通常会用到一些通用的STL算法如std::sort排序std::find查找std::for_each对容器中每个元素执行操作等。此外容器还提供了很多成员函数来操作元素如push_backpop_backinserterasebeginend等。 
cSTL算法的理解与使用sort, find, binary_search等 
C的STLStandard Template Library提供了一系列算法这些算法可以与STL容器配合使用以执行各种常见的任务如排序、查找和搜索。以下是对一些常见STL算法的理解与使用的简要说明 
std::sort排序 std::sort是一个通用排序算法可以对任何提供随机访问迭代器的序列进行排序。 
#include algorithm  
#include vector  
#include iostream  int main() {  std::vectorint numbers  {4, 2, 5, 3, 1};  // 使用std::sort对vector进行排序  std::sort(numbers.begin(), numbers.end());  // 输出排序后的vector  for (const auto num : numbers) {  std::cout  num   ;  }  return 0;  
}std::find查找 std::find用于在序列中查找特定元素。如果找到则返回指向该元素的迭代器否则返回序列的尾迭代器。 
#include algorithm  
#include vector  
#include iostream  int main() {  std::vectorint numbers  {1, 2, 3, 4, 5};  int target  3;  // 使用std::find查找target  auto it  std::find(numbers.begin(), numbers.end(), target);  if (it ! numbers.end()) {  std::cout  Found   target   at position:   std::distance(numbers.begin(), it)  std::endl;  } else {  std::cout  target   not found  std::endl;  }  return 0;  
}std::binary_search二分查找 std::binary_search用于在已排序的序列中执行二分查找。它要求序列必须是已排序的否则结果将是未定义的。 
#include algorithm  
#include vector  
#include iostream  int main() {  std::vectorint numbers  {1, 2, 3, 4, 5};  int target  3;  // 确保序列已排序  std::sort(numbers.begin(), numbers.end());  // 使用std::binary_search进行查找  bool found  std::binary_search(numbers.begin(), numbers.end(), target);  if (found) {  std::cout  target   found in the sorted sequence  std::endl;  } else {  std::cout  target   not found  std::endl;  }  return 0;  
}使用注意事项 std::sort默认使用操作符来比较元素但也可以传递自定义比较函数或lambda表达式。 std::find和std::binary_search返回的都是迭代器需要与容器的开始迭代器比较来确定是否找到了目标元素。 std::binary_search要求序列必须是已排序的否则会返回未定义的结果。 这些算法都提供了很大的灵活性因为它们是模板化的可以与任何类型的容器一起使用只要这些容器提供适当的迭代器类型。此外它们也可以很容易地与C的lambda表达式结合使用以提供自定义的比较逻辑。 文章转载自: http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.nqmhf.cn.gov.cn.nqmhf.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.rnzjc.cn.gov.cn.rnzjc.cn http://www.morning.rglzy.cn.gov.cn.rglzy.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.ysfj.cn.gov.cn.ysfj.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.trwkz.cn.gov.cn.trwkz.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.sjzsjsm.com.gov.cn.sjzsjsm.com http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.vehna.com.gov.cn.vehna.com http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.pqktp.cn.gov.cn.pqktp.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.hwpcm.cn.gov.cn.hwpcm.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.hjssh.cn.gov.cn.hjssh.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.yqwrj.cn.gov.cn.yqwrj.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.sxcwc.cn.gov.cn.sxcwc.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.mmjqk.cn.gov.cn.mmjqk.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.mlmwl.cn.gov.cn.mlmwl.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.mxhys.cn.gov.cn.mxhys.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.kphyl.cn.gov.cn.kphyl.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.gnyhc.cn.gov.cn.gnyhc.cn