โ ๋ฌธ์์ด
๋ฌธ์์ด(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๋ก ๋ณํํ๊ธฐ๋ ํ๋ค. ๋ฌธ์์ด์ ์ฃผ์๊ฐ์ ์ ๊ทผํด์ ๋ค๋ฃจ๋ค๋ณด๋ ์๋ณธ์ด ๋ณ๊ฒฝ๋ ์๋ ์๊ณ , ์๋ชป ์ ๊ทผ์ ์์คํ ์๋ฌ๋ฅผ ๋ฐ์์ํค๊ธฐ๋ ํ๋ค.
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
๋๊ธ