관리 메뉴

Mini

C++ split구현, 사용법 본문

Algorithm

C++ split구현, 사용법

Mini_96 2023. 6. 24. 19:39

문제 : c++ 에는 split함수가없다

해결 : 직접구현

주의1 : (pos=str.find(deli))에서 괄호를 싸줘야함.

주의 2: 마지막에 ret.push(input) => 마지막 남은 문자배열처리

 

* 로직

192.168.0.1

pos='.'의 idx인 3

token=192 (0부터, 3칸)

token넣기

지우기(192.) (0부터, 4칸)

#include<bits/stdc++.h>
using namespace std;

vector<string> split(string input, string deli) {
	long long pos=0;
	vector<string> ret;
	string token = "";
	while ((pos=input.find(deli)) != string::npos) {
		token = input.substr(0, pos);
		ret.push_back(token);
		input.erase(0, pos + deli.size());
	}
	//마지막 192.168.0.1 의 1저장
	ret.push_back(input);

	return ret;
}

int main()
{
	vector<string> input;
	input.push_back("192.168.0.1");
	input.push_back("125.0.0.1");

	vector<vector<string>> a;
	for (auto s : input) {
		a.push_back(split(s, "."));
	}

	for (auto q : a) {
		for (auto w : q) {
			cout << w << endl;
		}
	}


}

실행결과

 

'Algorithm' 카테고리의 다른 글

[알고리즘] 자바 전역 배열 선언 방법  (0) 2025.02.08
2차원 리스트 사용법  (0) 2022.08.23
순열 VS 조합 vs 부분집합 코드  (0) 2022.08.19
  (0) 2022.08.09