Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

codingfarm

C++ 문자열 snippet codes 본문

Algorithm

C++ 문자열 snippet codes

scarecrow1992 2025. 5. 28. 18:58

 

 

1. split

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> split(const string& str, char delimiter) {
    vector<string> result;
    size_t start = 0;  // 시작 인덱스
    size_t end;

    while ((end = str.find(delimiter, start)) != string::npos) {
        result.push_back(str.substr(start, end - start));  // 부분 문자열 추출
        start = end + 1;  // 다음 시작 지점으로 이동
    }

    // 마지막 토큰 처리
    result.push_back(str.substr(start));
    return result;
}

int main() {
    string text = "apple,banana,grape";
    vector<string> tokens = split(text, ',');

    for (const string& word : tokens) {
        cout << word << endl;
    }

    return 0;
}

python의 split을  C++로 구현한 함수.

 

2. stoi

문자열을 정수 타입으로 변환하는 함수이다.

int num = stoi("65");

 

 

 

'Algorithm' 카테고리의 다른 글

DFS 코드들  (0) 2025.05.25
Comments