본문 바로가기
Language & Library/etc

[C++] python의 join 함수 간단하게 구현

by 미네마네모 2019. 8. 14.

 

Python의 join 함수

l = ['a', 'b', 'c']
str = ",".join(l)

print(str) // a,b,c

 

C++에서 join 함수 구현

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
using namespace std;

int main() {
	
	std::vector<std::string> kbList;
	kbList.push_back("KB1234");
	kbList.push_back("test1");
	kbList.push_back("test2");

	std::ostringstream imploded;
	std::copy(kbList.begin(), kbList.end(),
           std::ostream_iterator<std::string>(imploded, ","));
    std::cout << imploded.str() << std::endl;
           
	return 0;
}

 

댓글