๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++ ๊ธฐ์ดˆ

[C++] 12. ๋ฌธ์ž์—ด(feat. ์ง๋ ฌํ™”)

by ์„œ์•„๋ž‘๐Ÿ˜ 2023. 5. 6.

string

 

 

โœ… ๋ฌธ์ž์—ด

  ๋ฌธ์ž์—ด(string)์€ ๋ฌธ์ž๋“ค์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•˜๊ฑฐ๋‚˜ ์ฃผ๊ณ  ๋ฐ›์„ ๋•Œ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. ๊ธฐ์กด C ์Šคํƒ€์ผ์˜ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ๋ฐฉ์‹์€ ์ง๊ด€์ ์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ ๊ธฐ์กด์˜ ๋ฐฉ์‹๋Œ€๋กœ ์“ฐ์—ฌ์ง„ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ฝ์„ ์ค„์€ ์•Œ์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๋‹จํžˆ ๋ณด๊ณ  ๋„˜์–ด๊ฐ€์ž.

 

#include <stdio.h>

int main()
{
    char str[16] = "Hello world";
    const char* str2 = "Hello Seoarang";

    char c = 'a';
    c -= 32;

    printf("%s\n", str);
    printf("%s\n", str2);
    printf("%c\n", c);
}

// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
//Hello world
//Hello Seoarang
//A

๋ฌธ์ž๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ž๋ฃŒํ˜•์€ char์ด๋‹ค. 1๋ฐ”์ดํŠธ ์ •์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์•„์Šคํ‚ค์ฝ”๋“œ์— ์˜ํ•ด ์ •์ˆ˜๊ฐ’์ด ๋ฌธ์ž๋กœ ์น˜ํ™˜๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  char์˜ ๋ฐฐ์—ด์ด๋‚˜ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์œ„ ์ฝ”๋“œ์—์„œ str์„ "Hi Google"๋กœ ๋ฐ”๊พธ๋ ค๋ฉด ๊นŒ๋‹ค๋กœ์šด ์ž‘์—…์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค. ๋˜ํ•œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ์ •ํ•ด์ ธ์žˆ์–ด์„œ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ์น˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ํ™œ์šฉํ•˜๊ธฐ์—” ์ œํ•œ์ด ์žˆ๋‹ค. ๋ณ€์ˆ˜ c๋Š” ์†Œ๋ฌธ์ž 'a'๋กœ ์ดˆ๊ธฐํ™”ํ–ˆ๊ณ  ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด 32๋ฅผ ๋นผ์คฌ๋‹ค. ์•„์Šคํ‚ค์ฝ”๋“œ์— ์˜ํ•œ ๋ณ€ํ™˜๋ฐฉ์‹์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์ธํ„ฐํŽ˜์ด์Šค ํ•จ์ˆ˜ ์—†์ด ์ˆซ์ž๋งŒ ์ ์–ด๋†จ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ์ฒ˜์Œ ๋ณธ ์‚ฌ๋žŒ์€ ์ฃผ์„์—†์ด ๋ฌด์Šจ ์ž‘์—…์ธ์ง€ ์•Œ๊ธธ์ด ์—†๋‹ค.

 

๋‹ค์Œ์€ ๋ชจ๋˜ C++๋ฐฉ์‹์˜ ๋ฌธ์ž์—ด์ด๋‹ค.

#include <iostream>

int main()
{
    std::string str1 = "Hello world";
    str1 = "Hi, Gooooooooooogle";


    std::cout << str1 << std::endl;
    std::cout << str1.length() << std::endl;
    std::cout << str1.find("o") << std::endl;
    std::cout << str1.substr(5) << std::endl;
}
// ์ถœ๋ ฅ ๊ฒฐ๊ณผ
//Hi, Gooooooooooogle
//19
//5
//ooooooooooogle

std::string์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ดˆ๊ธฐํ™”ํ–ˆ๊ณ  ์ดํ›„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค. std์—์„œ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋Š” string ๊ด€๋ จ ์ธํ„ฐํŽ˜์ด์Šค ๋ฉ”์†Œ๋“œ๊ฐ€ ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•˜๊ธฐ๊ฐ€ ๋งค์šฐ ํŽธํ•˜๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ ํŠน์ • ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ฑฐ๋‚˜, ๋ฌธ์ž์—ด์ด ๋น„์—ˆ๋Š”์ง€ ์ฒดํฌํ•˜๊ฑฐ๋‚˜, ํŠน์ • ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์ž๋ฅด๊ธฐ ๋“ฑ ๋งŽ์€ ๊ฒƒ๋“ค์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ ๋กœ std::string::c_str()ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด string ๋‚ด๋ถ€์˜ charํ˜• ํฌ์ธํ„ฐ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.(const char* ๋ฐ˜ํ™˜)

std::string์˜ ์ธํ„ฐํŽ˜์ด์Šค ๋ฉ”์†Œ๋“œ ๋ชฉ๋ก์€ ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜์ž.

https://en.cppreference.com/w/cpp/string/basic_string

 

std::basic_string - cppreference.com

The class template basic_string stores and manipulates sequences of character-like objects, which are non-array objects of trivial standard-layout type. The class is dependent neither on the character type nor on the nature of operations on that type. The

en.cppreference.com

 

 

โœ… ์ง๋ ฌํ™”(Serialization)

์ง๋ ฌํ™”๋ž€ ๊ฐ์ฒด์˜ ๋‚ด์šฉ์„ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํŒŒ์ผ ๋˜๋Š” ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด์„œ ์ŠคํŠธ๋ฆผ(์†ก์ˆ˜์‹ )์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ ์ƒ ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์ด๋‚˜ DB์— ์“ฐ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ๋ฐ˜๋Œ€๋กœ ํŒŒ์ผ์ด๋‚˜ DB์—์„œ ์ŠคํŠธ๋ฆผ์„ ์ฝ์–ด์™€์„œ ์ฝ”๋“œ ์ƒ์˜ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์„ ์—ญ์ง๋ ฌํ™”๋ผ๊ณ  ํ•œ๋‹ค.

๊ฐ์ฒด์˜ ๋‚ด์šฉ์„ ํŒŒ์ผ์ด๋‚˜ DB, ๋ฉ”๋ชจ๋ฆฌ ์ƒ์— ์ŠคํŠธ๋ฆผ ๋‹จ์œ„๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด C++ ๊ตฌ์กฐ์ฒด์˜ ๋ฉค๋ฒ„๋“ค์„ JSONํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์„ ๋•Œ(์ง๋ ฌํ™”) ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๋ฐ˜๋Œ€๋กœ JSON ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๊ฑฐ๋‚˜ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” JSON ๋‚ด์šฉ์„ ํŒŒ์‹ฑํ•  ๋•Œ, ๋ฌธ์ž์—ด์„ ๋จผ์ € ์ฝ์–ด ์˜จ ํ›„์— ์ฝ”๋“œ ํƒ€์ž…์— ๋งž๋„๋ก ํƒ€์ž… ์บ์ŠคํŒ…์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ๋ณด์ž.

// Serialization

...

Json::Value jsonData;
jsonData["op"] = "subscribe";

for (const auto &symbol : connectorInfo.targetSymbols)
    jsonData["args"].append("orderBookL2_25." + symbol);

const string &strJson = jsonData.toStyledString();
ws.send(strJson);

...

C++์—์„œ JSON๊ฐ์ฒด๋ฅผ ํŒŒ์‹ฑํ•˜๊ฑฐ๋‚˜ ๊ตฌ์„ฑํ•ด์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ jsoncpp๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ์œ„ ์ฝ”๋“œ๋Š” ์™„์ „ํ•œ ์ง๋ ฌํ™”๋ผ๊ณ  ํ•˜๊ธฐ์—” ์–ด๋ ต์ง€๋งŒ(๊ฐ์ฒด ๋‚ด์šฉ์„ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ), ์‰ฌ์šด ์˜ˆ์ œ๋ผ์„œ ๊ฐ€์ ธ์™”๋‹ค. json๋ฐ์ดํ„ฐ์— "op"์™€ "args"๋ฅผ key๋กœ ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ  ์›น์†Œ์ผ“ ์„œ๋ฒ„๋กœ sendํ•˜๋Š” ๋‚ด์šฉ์ด๋‹ค. 

 

// Deserialization
...

Json::Value jsonRoot;
if (!reader.parse(res, jsonRoot))
{
    LOG_ERROR(logger, "Failed to parse data(%s)", res.c_str());
    return false;
}

for (const auto &data : jsonRoot)
{
    const string side = data["side"].isNull() ? 0 : data["side"].asString();
    if (side != STR_BUY_CLOSE && side != STR_SELL_CLOSE)
        continue;

    const double price = data["price"].isNull() ? 0.0 : stod(data["price"].asString());
    const string clientOrderId = data["clientOrderId"].isNull() ? "" : data["clientOrderId"].asString();
    const string orderType = data["orderType"].isNull() ? "" : data["orderType"].asString();
    const double orgQty = data["origQty"].isNull() ? 0.0 : stod(data["origQty"].asString());

    ...
}

...

json ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑํ•œ ํ›„ ๋Œ€๋ถ€๋ถ„ string์œผ๋กœ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ (asString() ๋ฉ”์„œ๋“œ) double๊ฐ’์œผ๋กœ ์น˜ํ™˜ํ–ˆ๋‹ค(stod ์‚ฌ์šฉ). ์œ„ ์˜ˆ์ œ๋„ ์™„๋ฒฝํ•œ ์—ญ์ง๋ ฌํ™”๋Š” ์•„๋‹ˆ์ง€๋งŒ ์ง‘์ค‘ํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ๋•Œ ๋ฌธ์ž์—ด๋กœ ์ฃผ๊ณ  ๋ฐ›๋Š” ์ผ์ด ๋Œ€๋‹ค์ˆ˜์ด๋ฉฐ, ๋ฌธ์ž์—ด์„ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ๊ฐ€๊ณตํ•˜๋Š” ์ž‘์—…๋„ ๊ต‰์žฅํžˆ ๋นˆ๋ฒˆํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด์„ ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

 

 

 

โœ… ๋ฌธ์ž์—ด ์ œ์–ด ์ธํ„ฐํŽ˜์ด์Šค(c style)

c ๋ฐฉ์‹์˜ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜๋“ค๋„ ๋‹ค์–‘ํ•˜๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ strlen, strcpy, strcat, strcmp ๋“ฑ์ด ์žˆ๋‹ค. ๋ฌธ์ž์—ด ๊ธธ์ด๋‚˜ ๋ณต์‚ฌ, ์ž๋ฅด๊ธฐ, ๋น„๊ต๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค. atoi, atof์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์„ int, double๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ๋ฌธ์ž์—ด์˜ ์ฃผ์†Œ๊ฐ’์— ์ ‘๊ทผํ•ด์„œ ๋‹ค๋ฃจ๋‹ค๋ณด๋‹ˆ ์›๋ณธ์ด ๋ณ€๊ฒฝ๋  ์ˆ˜๋„ ์žˆ๊ณ , ์ž˜๋ชป ์ ‘๊ทผ์‹œ ์‹œ์Šคํ…œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ๋„ ํ•œ๋‹ค.

https://wikidocs.net/12188

 

11-01. ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜

[TOC] ## ๋Œ€ํ‘œ์  ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ํ—ค๋”ํ™”์ผ: string.h์™€ ctype.h * [string.h](http://www.cplusplus.com/reference/cst…

wikidocs.net

 

 

โœ… ๋ฌธ์ž์—ด ์ œ์–ด ์ธํ„ฐํŽ˜์ด์Šค(std::string)

std::string์€ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— ์ธํ„ฐํŽ˜์ด์Šค ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์šฐ์„  [] ์—ฐ์‚ฐ์ž๊ฐ€ ์˜ค๋ฒ„๋กœ๋”ฉ ๋˜์–ด์žˆ์–ด์„œ ํŠน์ • ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  standard library์˜ ์ปจํ…Œ์ด๋„ˆ ํŠน์„ฑ์ƒ iterator๊ธฐ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์›ํ•˜๋Š” ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒƒ๋“ค์„ ์ฐพ์•„๋‚ด๊ฑฐ๋‚˜ ํ•„ํ„ฐ๋งํ•˜๊ธฐ ์‰ฝ๋‹ค. ๋นˆ ๊ฐ’ ์ฒดํฌ์™€ ์‚ฌ์ด์ฆˆ, ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์ด์–ด๋ถ™์ด๊ฑฐ๋‚˜ ์ž๋ฅด๊ธฐ, ๊ฒ€์ƒ‰, ๋น„๊ต, ๋Œ€์ฒด ๋“ฑ ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์ด ์ธํ„ฐํŽ˜์ด์Šค ํ˜•ํƒœ๋กœ ์ œ๊ณต๋˜๊ณ  ์žˆ๋‹ค. stoi, stod๋Š” string์„ int์™€ double๋กœ ์น˜ํ™˜ํ•œ๋‹ค.

std::string์€ std::exception์„ ์ƒ์†๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ์ฒ˜๋ฆฌ๋ฉด์—์„œ ๊ธฐ๋ณธ ๋ฌธ์ž์—ด ํ•จ์ˆ˜๋ณด๋‹ค ์•ˆ์ „ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฌ์šด ์žฅ์ ์ด ์žˆ๋‹ค.

https://en.cppreference.com/w/cpp/string/basic_string

 

std::basic_string - cppreference.com

The class template basic_string stores and manipulates sequences of character-like objects, which are non-array objects of trivial standard-layout type. The class is dependent neither on the character type nor on the nature of operations on that type. The

en.cppreference.com

 

 

๋Œ“๊ธ€