吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2506|回复: 1
收起左侧

[C&C++ 转载] [stl][复习]string, list容器和迭代器的复习

[复制链接]
小可爱~ 发表于 2017-1-28 16:24
本帖最后由 小可爱~ 于 2017-1-28 16:31 编辑

[C++] 纯文本查看 复制代码
#define  _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <string>
#include <algorithm>                //算法的头文件

int main11()
{
        string s1("aaa");
        string s2 = "bbb";
        string s3 = s2 + "ccc";
        string s4(10, 'a');

        cout << s1 << endl;
        cout << s2 << endl;
        cout << s3 << endl;
        cout << s4 << endl;
        return 0;
}

//遍历string的元素
int main12()
{
        string s1 = "abcdefg";

        //1 数组模式
        for (int i = 0; i < s1.length(); i++)
        {
                cout << s1[i] << "  ";
        }
        cout << endl;
        //2 迭代器
        for (string::iterator it = s1.begin(); s1.end() != it; it++)
        {
                cout << *it << "  ";
        }
        cout << endl;
        //3 用 at 的方法获取元素
        try
        {
                for (int i = 0; i < s1.length(); i++)
                {
                        cout << s1.at(i) << "  ";
                }
        }
        catch (char c)
        {
                cout << c << endl;
        }
        catch (...)
        {
                cout << "发生异常" << endl;
        }
        
        cout << endl;
        return 0;
}

int main13()
{
        string s = "abcd";
        //1
        printf("%s\n", s.c_str());
        //2
        printf("%s\n", s.data());
        //3 内容拷贝
        char buf[128];
        //copy 参数一 数组, 参数二 copy几个 参数三 第几个位置拷贝开始
        //不拷贝  '\0'
        s.copy(buf, 2, 1);
        buf[2] = '\0';
        cout << "buf: " << buf << endl;
        return 0;
}

//string 字符串的链接
int main14()
{
        string s1 = "aaa";
        string s2 = "bbb";
        string s3 = s1 + s2;

        cout << s3 << endl;

        string s4 = "111";
        string s5 = "222";

        //添加  类似于 strcat
        s4.append(s5);

        cout << s4 << endl;
        return 0;
}

int main15()
{
        string s1 = "aaa, bbb, ccc, ddd, bbb, fff, ddd, bbb, yyy, bbb";

        int sVal = 0;
        //find 查找 参数一的字符串 , 参数二是第几个位置
        sVal = s1.find("bbb", sVal);
        while (sVal != string::npos)
        {
                cout << sVal << endl;
                sVal = sVal + 1;
                sVal = s1.find("bbb", sVal);
        }

        sVal = s1.find("bbb", 0);
        while (sVal != string::npos)
        {
                //替换 参数一第几个位置  参数二 替换几个 参数三替换成什么
                s1.replace(sVal, 3, "vvv");
                sVal = sVal + 1;
                sVal = s1.find("bbb", sVal);
        }
        return 0;
}

//string 区间删除
int main16()
{
        string s1 = "hello1 hello2 hell3, hello4";
        //找打这个迭代器 把这个迭代器所值的位置删除
        string::iterator it = find(s1.begin(), s1.end(), 'l');
        if (it != s1.end())
        {
                //删除迭代器所指的位置
                s1.erase(it);
        }
        cout << "erase success ? -- " << s1 << endl;
        return 0;
}

//插入字符串

int main17()
{
        string s1 = "cdefgh";
        //size_type 是 int 类型 一般存放位置   elEm *Pr一般存放指针
        s1.insert(0, "ab");
        //s1.insert(0, "ab", 2);
        cout << s1 << endl;
        return 0;
}

int main(int argv, char *args[])
{
        int ret = 0;
        //main11();
        //main12();
        //main13();
        //main14();
        //main15();
        //main16();
        main17();
        system("pause");
        return ret;
}



[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;
#include <list>
void main()
{
	list<int > List;			//定义一个链表
	//cout << "大小:" << List.size() << endl;//定义后的初始长度为 0
	for (int i = 0; i < 10; i++)
	{
		List.push_back(i + 1);
	}
	cout << "大小:" << List.size() << endl;
	list<int>::iterator it = List.begin();
	it++;
	it++;
	it++;
	List.insert(it, 1000);
	it = List.begin();
	while (it != List.end())
	{
		cout << *it++ << endl;
	}
	cout << "sort" << endl;
	List.sort();
	it = List.begin();
	while (it != List.end())
	{
		cout << *it++ << endl;
	}

	for (list<int>::iterator p = List.begin(); p != List.end(); p++)
	{
		cout << *p << endl;
	}

	system("pause");
}

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
liptm304 + 2 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

qwertyui 发表于 2017-1-28 18:14
来看看。学习了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 08:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表