C++string 용기 기본 개념 상세 설명
본질:
string 은 C++스타일 의 문자열 이 고 string 은 본질 적 으로 클래스 입 니 다.
string 과 char*의 차이 점:
4.567917.char*는 지침 입 니 다.
4.567917.string 은 클래스 내부 에 char*를 밀봉 하고 이 문자열 을 관리 하 며 char*형의 용기 입 니 다특징:
string 클래스 내부 에 많은 멤버 를 봉인 하 는 방법
예 를 들 어 find 찾기,복사 복사,delete 삭제,replace 교체,insert 삽입
string 은 char*에서 분 배 된 메모 리 를 관리 합 니 다.복제 경계 와 수치 경계 등 을 걱정 하지 않 고 클래스 내부 에서 책임 을 집 니 다.
string 구조 함수
#include <iostream>
using namespace std;
#include<string>
//string
void test01()
{
//
string s1;
//string(const char* s); s
const char* str = "hello,world";
string s2(str);
cout << "s2= " << s2 << endl;
//string(const string& str) string string
string s3(s2);
cout << "s3= " << s3 << endl;
//string(int n,char c); n c
string s4(10, 'a');
cout << s4 << endl;
}
int main()
{
test01();
}string 할당 작업기능 설명:
string 문자열 에 값 을 부여 합 니 다.
#include <iostream>
using namespace std;
#include<string>
//string
void test01()
{
//string& operator=(const char* s)//char*
string str1;
str1 = "hello,world";
cout << "str1=" <<str1<< endl;
//string& operator=(const string &s);// s
string str2;
str2 = str1;
cout << "str2= " << str2 << endl;
//string& operator=(char c);//
string str3;
str3 = 'a';
cout << "str3= " << str3 << endl;
//string& assign(const char *s);//
string str4;
str4.assign("hello C++");
cout << "str4= " << str4 << endl;
//string& assign(const char *s,int n);// s n
string str5;
str5.assign("hello C++", 5);
cout << "str5= " << str5 << endl;
//string& assign(const string &s);// s
string str6;
str6.assign(str5);
cout << "str6= " << str6 << endl;
string str7;
str7.assign(10, 'w');
cout << "str7= " << str7 << endl;
}
int main()
{
test01();
}string 문자열 연결기능 설명:
문자열 끝 에 문자열 을 맞 추 는 것 을 실현 합 니 다.
#include <iostream>
using namespace std;
#include<string>
//string
void test01()
{
// : +=
string str1 = " ";
str1 += " ";
cout << "str1= " << str1 << endl;
str1 += ':';
cout << "str1= " << str1 << endl;
string str2 = "LOL";
str1 += str2;
cout << "str1= " << str1 << endl;
// s
string str3 = "I";
str3.append("love");
cout << "str3= " << str3 << endl;
// s n
str3.append("game abcde", 4);
cout << "str3= " << str3 << endl;
// append
str3.append(str2);
cout << "str3= " << str3 << endl;
// s pos n
str3.append(str2, 0, 3);// 2: , 3
cout << "str3= " << str3 << endl;
}
int main()
{
test01();
}string 찾기 및 교체 작업기능 설명:
#include <iostream>
using namespace std;
#include<string>
//
//1.
void test01()
{
string str1 = "abcdefg";
int pos=str1.find("de");
if (pos == -1)
{
cout << " " << endl;
}
else
{
cout << " pos: " << pos << endl;
}
//rfind
pos=str1.rfind("de");
cout << "pos= " << pos << endl;
//rfind find
//rfind ,find
}
//2.
void test02()
{
string str1 = "abcdefg";
//replace , , ,
// 1 3 "1111"
str1.replace(1, 3, "1111");
cout << "str1= " << str1 << endl;
}
int main()
{
test01();
test02();
}string 문자열 비교기능 설명:
문자열 간 비교
<귀환-1
#include <iostream>
using namespace std;
#include<string>
//
void test01()
{
string str1 = "A";
string str2 = "a";
if (str1.compare(str2) == 0)
{
cout << "str1 str2" << endl;
}
else if (str1.compare(str2) > 0)
{
cout << "str1 str2" << endl;
}
else if (str1.compare(str2) < 0)
{
cout << "str1 str2" << endl;
}
}
int main()
{
test01();
}string 문자 액세스string 의 단일 문자 접근 방식 은 두 가지 가 있 습 니 다.
#include <iostream>
using namespace std;
#include<string>
//string
void test01()
{
string str = "hello";
cout << "str= " <<str << endl;
// []
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << endl;
}
// at
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << endl;
}
//
str[0] = 'x';
str.at(1) = 'x';
cout << "str= " << str << endl;
}
int main()
{
test01();
}string 삽입 및 삭제기능 설명:
string 문자열 을 삽입 하고 삭제 합 니 다.
#include <iostream>
using namespace std;
#include<string>
//
void test01()
{
string str = "hello";
//
str.insert(1, "111");
cout << "str= " << str << endl;
//
str.erase(1, 3);
cout << "str= " << str << endl;
}
int main()
{
test01();
}문자열 문자열기능 설명:
문자열 에서 원 하 는 하위 문자열 가 져 오기'
#include <iostream>
using namespace std;
#include<string>
//string
void test01()
{
string str = "abcdef";
string subStr = str.substr(1, 3);
cout << "subStr= " << subStr << endl;
}
int main()
{
test01();
}C++string 용기 의 기본 개념 에 대한 상세 한 설명 을 담 은 이 글 은 여기까지 입 니 다.더 많은 C++string 용기 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
알고리즘 파트 3 : 스택을 사용하여 문자열 반전얘들 아. 오늘은 스택을 사용하여 문자열을 뒤집는 방법을 보여드리겠습니다. 이 문제에서 우리는 하나의 문자열을 받았고 스택을 사용하여 그것을 뒤집어야 합니다. 해결책 이것이 도움이 되길 바랍니다. 감사합니다 ❤....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.