STL
1. container(컨테이너)
ㄴ개거체들을 저장하는 객체나 클래스
vector(배열), list(링크드 리스트), deque(덱), string(문자열 전용 컨테이너)<-지금 이거, map 등
2. literator(반복자)
ㄴ컨테이너에 저장된 요소를 순회하고 접근하는 객체나 클래스
3. 알고리즘(algorithm)
ㄴ데이터를 다루기 위한 함수
find, sort, search 등
4. 함수 객체(function object), 함수자(functor)
ㄴ함수처럼 동작하는 객체로, operator() 연산자를 중첩한 클래스의 객체
string
//typedef basic_strig <char> string;
#include <iostream>
#include <string>
using namespace std;
int main(void){
string str = "안녕!";
cout << str << endl;
str.push_back('H');//<"H" 는 안되고 'H'는 된다.
str.push_back('i');
cout << str << endl;//안녕!Hi
for (int i = 0; i < str.size(); i++)//안녕!Hi
cout << str[i];
return 0;
}
#include <string>을 해야한다.
string클래스의 str객체이고
.push_back이 가능하다(이때 작은따옴표' '를 써야한다.)
.size()도 가능하고
str[i]로 참조가 가능하다.
string:
string는 문자만을 원소로 저장하고 문자열을 조작할 목적으로 사용되는 컨테이너이다.
C/C++ 문자열처럼 마지막에 '\0'을 포함해야하는 요구사항은 없다.
생성자 | |
string s | 기본 생성자로 s 생성 |
string s(str) | str 문자열로 s 생성 |
string s(str, n) | str 문자열에서 n개의 문자로 s를 생성 |
string s(n, c) | n개의 c문자로 s를 생성 |
string s(iter1, iter2) | 반복자 구간 [iter1, iter2)의 문자로 s를 생성 |
string s(p1, p2) | 포인터 구간 [p1, p2)의 문자로 s를 생성 |
string s;//어떻게 나올까?
string s1("hello");//hello로 나오겠지?
string s2("hello", 1);//h로 나오겠지?
string s3("hello", 6);//어떻게 나올까?(초과시켰음)>>오류 없이 hello로 나옴
string s4(3, 'a');//aaa로 나오겠지?
cout << s<<"\n";
cout << s1<<"\n";
cout << s2<<"\n";
cout << s3<<"\n";
cout << s4<<"\n";
참고 :
https://cplusplus.com/reference/string/string/?kw=string
https://cplusplus.com/reference/string/string/?kw=string
cplusplus.com
'c++' 카테고리의 다른 글
[C++] STL - <algorithm> (0) | 2023.02.25 |
---|---|
[c++] STL (0) | 2023.02.20 |
[c++] STL - 컨테이너 - vector (0) | 2023.02.20 |
[C++] 내림 함수(floor), 올림 함수(ceil) (0) | 2023.02.20 |
[c++] c++루트 구하기 sqrt함수 (0) | 2023.02.20 |