欢迎使用优快云-markdown编辑器

本文详细介绍了C++ STL中的迭代器概念、使用求和函数accumulate、数组操作、排序算法sort、字符串输入输出、尾部添加与删除字符、查找与转换数字、set容器与map容器的应用,以及字符串与数值间的相互转换。

[转]C++STL 常用 函数 用法

  整理自《ACM程序设计》  

  迭代器(iterator)
  个人理解就是把所有和迭代有关的东西给抽象出来的,不管是数组的下标,指针,for里面的、list里面的、vector里面的,抽象一下变成了iterator
View Code

  
  求和( accumulate)
  accumulate(v.begin(),v.end(),0),把从 v.begin() 开始到 v.end()结束所有的元素加到 0上面去
1 #include
2 #include
3 #include
4
5 using namespace std;
6
7 int main()
8 {
9 vector v;
10 for(int i = 0; i < 10; ++i )
11 {
12 v.push_back(i);
13 }
14 for(vector::iterator it = v.begin(); it != v.end(); ++it)
15 {
16 cout << *it << ” “;
17 }
18 cout << endl;
19 cout << accumulate(v.begin(),v.end(),0) << endl;
20 return 0;
21 }

  vector(动态数组)
  vector有内存管理的机制,也就是说对于插入和删除,vector可以动态调整所占用的内存空间。  
  vector相关函数
1 #include
2 #include
3
4 using namespace std;
5
6 int main()
7 {
8 vector v;
9 v.push_back(3); //数组尾部插入310 v.push_back(2);
11 v.push_back(1);
12 v.push_back(0);
13 cout << ” 下标 ” << v[3] << endl;
14 cout << ” 迭代器 ” << endl;
15 for(vector::iterator i = v.begin();i!= v.end();++i)
16 {
17 cout << *i << ” “;
18 }
19 cout << endl;
20 //在第一个元素之前插入111 insert begin+n是在第n个元素之前插入21 v.insert(v.begin(),111);
22 //在最后一个元素之后插入222 insert end + n 是在n个元素之后插入23 v.insert(v.end(),222);
24
25 for(vector::iterator i = v.begin();i!= v.end();++i)
26 {
27 cout << *i << ” “;
28 }
29 cout << endl;
30
31 vector arr(10);
32 for(int i = 0; i < 10; i++)
33 {
34 arr[i] = i;
35 }
36 for(vector::iterator i = arr.begin();i!= arr.end();++i)
37 {
38 cout << *i << ” “;
39 }
40 cout << endl;
41
42 //删除 同insert43 arr.erase(arr.begin());
44
45 for(vector::iterator i = arr.begin();i!= arr.end();++i)
46 {
47 cout << *i << ” ” ;
48 }
49 cout << endl ;
50
51 arr.erase(arr.begin(),arr.begin()+5);
52
53 for(vector::iterator i = arr.begin();i!= arr.end();++i)
54 {
55 cout << *i << ” ” ;
56 }
57 cout << endl ;
58 return 0 ;
59 }

  数组转置 ( reverse)
  reverse(v.begin(),v.end())
View Code

  排序( sort)

  sort(v.begin(),v.end())
1 #include
2 #include
3 #include
4
5 using namespace std;
6
7 bool Comp(const int &a,const int &b)
8 {
9 return a>b;
10 }
11
12 int main()
13 {
14 vector v;
15 v.push_back(1);
16 v.push_back(3);
17 v.push_back(2);
18 v.push_back(55);
19 v.push_back(-1);
20 v.push_back(0);
21 v.push_back(2);
22 v.push_back(3);
23 v.push_back(4);
24
25 for(vector::iterator it = v.begin(); it != v.end(); ++it)
26 {
27 cout << *it << ” “;
28 }
29 cout << endl;
30
31 //默认升序32 sort(v.begin(),v.end());
33
34
35 for(vector::iterator it = v.begin(); it != v.end(); ++it)
36 {
37 cout << *it << ” “;
38 }
39 cout << endl;
40
41 //用降序 需要自定义一个降序函数42 sort(v.begin(),v.end(),Comp);
43
44
45 for(vector::iterator it = v.begin(); it != v.end(); ++it)
46 {
47 cout << *it << ” “;
48 }
49 cout << endl;
50
51 return 0;
52 }

  字符串()
  输入
1 #include
2 #include
3 #include
4
5 using namespace std;
6
7 int main()
8 {
9 string s1;
10 s1 = “hello”;
11
12 string s2;
13 char s[1024];
14 //scanf 输入速度比cin快的多
15 //scanf 是C函数,不可以输入string16 scanf(“%s”,s);
17 s2 = s;
18
19 cout << s1 << endl;
20 cout << s2 << endl;
21
22 return 0;
23 }

 
  尾部添加字符字符串直接用+号 例如: s += ‘a’; s += “abc”,或者使用append方法,s.append(“123”)
  删除 (erase clear)
  s.erase(it + 1,it + 4); clear()
View Code

  查找(find)
  用find找到string里面第一个要找到元素(char或者串),找到返回数组下标,找不到返回end()迭代器
  string和vector有很多相同的东西,比如length(),size(),empty(),reverse(),相对也容易,就不一一说了。
  数字化处理(string)
  经常会遇到这样一种情况,有一个数字,需要把每一位给提取出来,如果用取余数的方法,花费的时间就会很长,所以可以当成字符串来处理,方便、省时。
  例子:求一个整数各位数的和
1 #include
2 #include
3
4 using namespace std;
5
6 int main()
7 {
8 string s;
9 s = “123456789”;
10 int sum = 0;
11 for(int i = 0; i < s.size(); ++i)
12 {
13 switch(s[i])
14 {
15 case ‘1’: sum += 1;break;
16 case ‘2’: sum += 2;break;
17 case ‘3’: sum += 3;break;
18 case ‘4’: sum += 4;break;
19 case ‘5’: sum += 5;break;
20 case ‘6’: sum += 6;break;
21 case ‘7’: sum += 7;break;
22 case ‘8’: sum += 8;break;
23 case ‘9’: sum += 9;break;
24 }
25 }
26
27 cout << sum << endl;
28
29 return 0;
30 }

  string与char *
  
View Code
  sscanf
  
View Code

  string与数值相互转换( sprintf )
1 #include
2 #include
3 #include
4 #include
5
6 using namespace std;
7
8 //c++ 方法 把数转换为string 9 string converToString(double x)
10 {
11 ostringstream o;
12 if( o << x)
13 {
14 // str()没有’\0’ c_str有15 return o.str();
16 }
17 return “error”;
18 }
19
20 double converFromString(const string &s)
21 {
22 istringstream i(s);
23 double x;
24 if( i >> x)
25 {
26 return x;
27 }
28 //if error29 return 0.0;
30 }
31 int main()
32 {
33 char b[100];
34 string s1;
35
36 //c语言方法37 sprintf(b,”%d”,1987);
38 s1 = b;
39 cout << s1 << endl;
40
41 string s2 = converToString(1954);
42 cout << s2 << endl;
43
44 string s3 = “202”;
45 int c = converFromString(s3);
46 cout << c << endl;
47
48 string s4 = “casacsa6”;
49 int d = converFromString(s4);
50 cout << d << endl;
51
52 string s5 = “21abf4”;
53 int f = converFromString(s5);
54 cout << f << endl;
55
56 return 0;
57 }

  set容器
  set是用红黑树的平衡二叉索引树的数据结构来实现的,插入时,它会自动调节二叉树排列,把元素放到适合的位置,确保每个子树根节点的键值大于 左子树所有的值、小于右子树所有的值,插入重复数据时会忽略。set迭代器采用中序遍历,检索效率高于vector、deque、list,并且会将元素 按照升序的序列遍历。set容器中的数值,一经更改,set会根据新值旋转二叉树,以保证平衡,构建set就是为了快速检索(python中的set一旦 建立就是一个常量,不能改的)。

  multiset,与set不同之处就是它允许有重复的键值。
  正反遍历,迭代器iterator、reverse_iterator
1 #include
2 #include
3
4 using namespace std;
5
6 int main()
7 {
8 set v;
9 v.insert(1);
10 v.insert(3);
11 v.insert(5);
12 v.insert(2);
13 v.insert(4);
14 v.insert(3);
15
16 //中序遍历 升序遍历17 for(set::iterator it = v.begin(); it != v.end(); ++it)
18 {
19 cout << *it << ” “;
20 }
21 cout << endl;
22
23 for(set::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
24 {
25 cout << *rit << ” “;
26 }
27 cout << endl;
28
29 return 0;
30 }

  自定义比较函数,insert的时候,set会使用默认的比较函数(升序),很多情况下需要自己编写比较函数。
  1、如果元素不是结构体,可以编写比较函数,下面这个例子是用降序排列的(和上例插入数据相同):
View Code

  2、元素本身就是结构体,直接把比较函数写在结构体内部,下面的例子依然降序:
View Code

  multiset与set的不同之处就是key可以重复,以及erase(key)的时候会删除multiset里面所有的key并且返回删除的个数。

  map
  map也是使用红黑树,他是一个键值对(key:value映射),便利时依然默认按照key程序的方式遍历,同set。

View Code

  用map实现数字分离
  string –> number
  之前用string进行过数字分离,现在使用map
View Code

  number –> string
View Code

  multimap
  multimap由于允许有重复的元素,所以元素插入、删除、查找都与map不同。
  插入insert(pair

多源动态最优潮流的分布鲁棒优化方法(IEEE118节点)(Matlab代码实现)内容概要:本文介绍了基于Matlab代码实现的多源动态最优潮流的分布鲁棒优化方法,适用于IEEE118节点电力系统。该方法结合两阶段鲁棒模型与确定性模型,旨在应对电力系统中多源输入(如可再生能源)的不确定性,提升系统运行的安全性与经济性。文中详细阐述了分布鲁棒优化的建模思路,包括不确定性集合的构建、目标函数的设计以及约束条件的处理,并通过Matlab编程实现算法求解,提供了完整的仿真流程与结果分析。此外,文档还列举了大量相关电力系统优化研究案例,涵盖微电网调度、电动汽车集群并网、需求响应、储能配置等多个方向,展示了其在实际工程中的广泛应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事能源系统优化工作的工程师。; 使用场景及目标:①用于研究高比例可再生能源接入背景下电力系统的动态最优潮流问题;②支撑科研工作中对分布鲁棒优化模型的复现与改进;③为电力系统调度、规划及运行决策提供理论支持与仿真工具。; 阅读建议:建议读者结合提供的Matlab代码与IEEE118节点系统参数进行实操演练,深入理解分布鲁棒优化的建模逻辑与求解过程,同时可参考文中提及的其他优化案例拓展研究思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值