c:라이브러리_추가가_필요한_자료형

This is an old revision of the document!


이 문서에서는 추가 라이브러리의 include가 필요한 자료형을 다룬다.

자료 유형 속해 있는 라이브러리 역할 비고
string <string> 문자열을 더 쉽게 다룰 수 있다. -
vector <vector> 기존의 배열을 더 쉽게 다룰 수 있다. -
map <map> key:value 쌍을 이용할 수 있다. -
unordered_map <unordered_map> map과 같지만 key 기준 정렬이 일어나지 않아 정렬이 필요하지 않은 경우 성능상 map보다 유리하다. -
pair <utility> 2개의 자료형을 하나의 객체로 묶을 수 있다. <vector> 라이브러리에서 <utility>를 include 하고 있으므로 <vector>를 include 했다면 따로 할 필요가 없다.
tuple <tuple> 3개 이상의 자료형을 하나의 객체로 묶을 수 있다. -
deque <deque> vector와 비슷하지만 pop_front(), pop_back() 같은 함수를 지원하여 가장 첫 원소 혹은 가장 마지막 원소만 제거할 수 있다. -

<string> 라이브러리에 있는 자료형으로 문자열을 좀 더 쉽게 다룰 수 있다. 한 예로 + 연산자로 서로 다른 문자열을 합칠 수 있다. 포함되어 있는 함수는 매우 많으나 주로 사용하는 몇 가지만 설명한다.

함수명 함수 설명 예시
to_string(x) int나 double 같은 숫자형 자료를 string 자료형으로 변환하여 반환한다. to_string(3) $\rightarrow$ “3”
to_string(3.8) $\rightarrow$ “3.8”
stoi(x) string 자료형을 int형 정수로 변환하여 반환한다. stoi(“3”) $\rightarrow 3
stoi(“3.8”) $\rightarrow$ 3
stod(x) string 자료형을 double형 실수로 변환하여 반환한다. stod(“3”) $\rightarrow$ 3.0
stod(“3.8”) $\rightarrow$ 3.8
string.substr(start, count) 문자열을 start 위치부터 start+count-1 위치까지 잘라서 string 자료형으로 반환한다. “abcde”.substr(1,3) $\rightarrow$ “bcd”
string.c_str() string 자료형인 문자열을 const char형 포인터로 변환하여 반환한다. “abcde”.c_str() $\rightarrow$ “abcde”(type is const char*)

substr의 경우 인자가 하나만 있으면 start위치부터 끝까지를 반환한다.

다음은 각 함수를 사용하는 코드다.

| example_string.cpp
#include <iostream>
#include <string>
#include <typeinfo>
 
using std::cout;
using std::string;
using std::to_string;
using std::stoi;
using std::stod;
 
int main()
{
    cout.precision(17);
 
    string str1 = "1234.56789";
 
    int x = stoi(str1);// x = 1234
    double xd = stod(str1); // xd = 1234.56789
 
    string i_to_s = to_string(x); // i_to_s = "1234"
    string d_to_s = to_string(xd); // d_to_s = "1234.56789"
 
    string sub1 = str1.substr(5);// sub1 = "56789"
    string sub2 = str1.substr(5,3);// sub2 = "567"
    string sub3 = sub1 + sub2;// sub3 = "56789567"
 
    const char *ch = str1.c_str(); // ch = "1234.56789"
 
    cout << "str: " << str1 << "\t type: " << typeid(str1).name() << "\n";
    cout << "str to int: " << x << "\t type: " << typeid(x).name() << "\n";
    cout << "str to double: " << xd << "\t type: " << typeid(xd).name() << "\n";
    cout << "substr one parameter: " << sub1 << "\t type: " << typeid(sub1).name() << "\n";
    cout << "substr two parameter: " << sub2 << "\t type: " << typeid(sub2).name() << "\n";
    cout << "string + operation: " << sub3 << "\t type: " << typeid(sub3).name() << "\n";
    cout << "str to char array: " << ch << "\t type: " << typeid(ch).name() << "\n";
}

실행결과는 다음과 같다.

str: 1234.56789 $\quad$ type: std::_ _cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
str to int: 1234 $\quad$ type: int
str to double: 1234.56789 $\quad$ type: double
substr one parameter: 56789 $\quad$ type: std::_ _cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
substr two parameter: 567 $\quad$ type: std::_ _cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
string + operation: 56789789 $\quad$ type: std::_ _cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >
str to char array: 1234.56789 $\quad$ type: char const*

g++의 경우 typeid를 나름대로 define하여 쓰고 있어 그냥 실행하면 int는 i로 double은 d로 const char* 는 PKc로 나타난다. 위와 같이 정확한 명칭으로 나타나게 하기 위해서는

./example_string | c++filt --types

형식으로 실행을 해야한다.

vector는 다루기 쉬운 동적배열 자료형이다.

vector에서 지원하는 함수는 다음과 같다.

함수명 함수 설명
vector.begin() vector의 첫 요소의 주소를 가리키는 포인터를 반환한다.
vector.end() vector의 마지막요소 +1 번째 요소(null값)의 주소를 가리키는 포인터를 반환한다.
vector.capacity() vector의 크기를 반환한다.
vector.size() vector가 실제로 사용하고 있는 크기를 반환한다.
vector.at(x) vector의 x번째 요소를 반환한다. 잘못된 위치를 넣으면 컴파일 타임에서 에러가 발생한다.
vector.push_back(x) vector의 마지막 요소 다음에 x를 추가한다.
  • c/라이브러리_추가가_필요한_자료형.1642569436.txt.gz
  • Last modified: 2023/09/05 15:46
  • (external edit)