首页 > 其他分享 >STL之string

STL之string

时间:2024-09-04 14:54:49浏览次数:4  
标签:count begin string iterator STL s1 迭代

目录

string常见的构造

迭代器

正向迭代器

使用方法

反向迭代器

const正向/反向迭代器

capacity接口

reserve 

 resize

string类对象的修改操作

 insert

头插

中间位置插入

erase

仅翻转字母

isalpha

 字符串中第一个唯一字符


string常见的构造

string s1("hello");
string s2(s1, 1, 2);
string s3(s1, 1, string::npos);
cout << s2 << endl;
cout << s3 << endl;

迭代器

正向迭代器

string::iterator;

iterator在string的类域中

使用方法

string::iterator it = s1.begin();
while (it != s1.end())
{
	cout << *it << endl;
	it++;
}

反向迭代器

string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
	cout << *rit << " ";
	rit++;
}

const正向/反向迭代器

int String2int(const string& num)
{
	string::const_iterator cit = num.begin();
	int count = 0;
	while (cit != num.end())
	{
		count = count * 10 + *cit - '0';
		++cit;
	}
	return count;
}

capacity接口

s1.clear();

大小减为0但capacity任然存在。

reserve 

往往会多开一些   最后一个位置放\0

 resize

会改变size和capacity,超出的位置用\0

s.resize(100,'x');

也可以自定义

string类对象的修改操作

 +=相比push_back和append用起来更舒服。

 insert

头插

s.insert(s.begin(),'x');

中间位置插入

s.insert(2,"2");

erase

s.erase(2,3);

从第二个位置开始删除三个字符。

仅翻转字母

917. 仅仅反转字母icon-default.png?t=N7T8https://leetcode.cn/problems/reverse-only-letters/

class Solution {
public:
    string reverseOnlyLetters(string s) {
        string::iterator begin=s.begin(),end=s.end();
        while(begin<end)
        {
            while(begin<end&&!isalpha(*begin))
                begin++;
            while(begin<end&&!isalpha(*end))
                end--;
            swap(*begin,*end);
            ++begin,--end;
        }
        return s;
    }
};

思考:这段代码用数组咋实现?

isalpha

C/C++库函数(isalpha)判断字符是否为字母_c++ 判断 全英文-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qq_42410605/article/details/102508916

 字符串中第一个唯一字符

387. 字符串中的第一个唯一字符icon-default.png?t=N7T8https://leetcode.cn/problems/first-unique-character-in-a-string/

class Solution {
public:
    int firstUniqChar(string s) {
        vector<int> count(26,0);
        for(auto ch:s)
            count[ch-'a']++;
        for(int i=0;i<s.size();i++)
        {
            if(count[s[i]-'a']==1)
                return i;
        }
        return -1;
    }
};

尝试vector的构造方法有c的定义方式过度到c++.

标签:count,begin,string,iterator,STL,s1,迭代
From: https://blog.csdn.net/2301_77479435/article/details/141823138

相关文章

  • c++ string类 重载实现(续)9月3日
    #include<iostream>#include<string>#include<cstring>classMystring{ private: intlen; char*str; public: Mystring() { str=nullptr; len=0; } Mystring(constchar*s) { len=strlen(s); str=newchar[len+1]; strcpy(s......
  • SQLServer2017对象名STRING_SPLIT无效
    SQLServer2017在使用“STRING_SPLIT”方法时报错:  1select*fromSTRING_SPLIT('1,2,3,4,5',',')  12消息208,级别16,状态1,第3行对象名'STRING_SPLIT'无效。原因STRING_SPLIT方法要求数据库的兼容级别至少为130。当级别小于130时,SQ......
  • string字符串
    string字符串1.翻转字符串#include<bits/stdc++.h>usingnamespacestd;intmain(){ strings; getline(cin,s); /*方法1.reverse搭配迭代器 reverse(s.begin(),s.end()); cout<<s; */ /*方法2.反着输出 for(inti=s.length()-1;i>=0;i--){ cout<<s[i]; }*/ //方法3.一半交换 ......
  • C++STL
    1.1STL初识STL(StandardTemplateLibrary,标准模板库)STL从广义上分为:容器(container)算法(algorithm)迭代器(iterator)容器和算法之间通过迭代器进行无缝连接STL几乎所有代码都采用了模板类或者模板函数1.2STL六大组件STL大体分为六大组件,分别是:容器、算法、迭代器、仿函数、适配器......
  • 20240903mystring进阶
    #include<iostream>#include<cstring>#include<stdexcept>//Forstd::out_of_rangeclassMystring{public://默认构造函数Mystring():str(nullptr),len(0){}//有参构造函数Mystring(constchar*s){len=strlen(......
  • 【已解决】Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.
    一、问题描述Invalidvaluetypeforattribute‘factoryBeanObjectType‘:java.lang.String二、解决方案更新本地的Mybatisplus版本<dependency>  <groupId>com.baomidou</groupId>  <artifactId>mybatis-plus-spring-boot3-starter</artifactId> ......
  • C++STL之list容器:基本使用及模拟实现
    目录有了vector,为何还需listlist的使用1,push_back、push_front、pop_back、pop_front的使用2,正向、反向、const正向、const反向迭代器的使用正向、反向迭代器的使用const正向、const反向迭代器的使用3,operator=赋值4,insert、erase任意位置的插入、删除5,迭代器失效(......
  • CF 2100-2400 strings 乱做
    CF1995DCases显然如果选了某个字符那么不妨选它出现的所有位置。check方式等价于相邻两个选择的位置间距\(\lek\),等价于连续\(k\)个必须选一个(最后一个必须选)枚举位置维护字符集是做不了的,状态数\(O(n2^c)\)无法优化考虑枚举字符集\(s\)。设原串连续\(k\)个字符的字......
  • 使用C++,仿照string类,实现myString
    类由结构体演化而来,只需要将struct改成关键字class,就定义了一个类C++中类和结构体的区别:默认的权限不同,结构体中默认权限为public,类中默认权限为private默认的继承方式不同,结构体的默认继承方式为public,类的默认继承方式为private//定义格式class类名{public:......
  • JAVA List<Map<String, Object>> sort 多个排序写法
     基本方法/***排序=**@paramlist*@paramsort_key*@return*/publicstaticList<Map<String,Object>>sort(List<Map<String,Object>>list,Stringsort_key,Booleanasc,Stringsort_key2,Boole......