C++¿¡¼ std::stringÀÇ ¹®ÀÚ¿À» ÅäÅ«¿¡ ÀÇÇØ Àß¶ó º¸ÀÚ.string::find_first_of ÇÔ¼ö¿¡ ´ëÇغñÁÖ¾ó½ºÆ©µð¿À 17 ÇÔ¼ö ¿øÇüsize_type find_first_of(const basic_string& _Right, const size_type _Off = 0) _Right ¸Å°³º¯¼ö´Â ÅäÅ«À¸·Î ¹®ÀÚµéÀÇ ÁýÇÕÀÌ´Ù. "abcde" ¹®ÀÚ¿Áß¿¡ Çϳª¶óµµ ÀÏÄ¡ÇÏ´Â ¹®ÀÚ°¡ ÀÖÀ¸¸é À§Ä¡¸¦ ¸®ÅÏÇÑ´Ù. ÅäÅ«ÀÌ ""ó·³ ºó¹®ÀÚ¿À̸é std::string::npos¸¦ ¸®ÅÏÇÑ´Ù. ÅäÅ«À» ãÀ»¼ö ¾øÀ¸¸é std::string::npos¸¦ ¸®ÅÏÇÑ´Ù. string str = "Hello world";
string tok = "@"; string::size_type pos = str.find_first_of(tok); if(pos == std::string::npos) cout << "Position of find_first_of std::string::npos " << endl; else cout << "Position of find_first_of is " << pos << endl; °á°ú) Position of find_first_of is 1 string::find ÇÔ¼ö¿¡ ´ëÇØ ºñÁÖ¾ó½ºÆ©µð¿À 17 ÇÔ¼ö ¿øÇü size_type find(const basic_string& _Right, const size_type _Off = 0) _Right ¸Å°³º¯¼ö´Â ÅäÅ«À¸·Î Àüü ¹®ÀÚ¿ÀÌ´Ù. "llo" Àüü ¹®ÀÚ¿ÀÌ ÀÏÄ¡Çϸé À§Ä¡¸¦ ¸®ÅÏÇÑ´Ù. ÅäÅ«ÀÌ ""ó·³ ºó¹®ÀÚ¿À̸é '0'À» ¸®ÅÏÇÑ´Ù. ÅäÅ«À» ãÀ»¼ö ¾øÀ¸¸é std::string::npos¸¦ ¸®ÅÏÇÑ´Ù. string str = "Hello world";
string tok = "llo"; string::size_type pos = str.find(tok); if(pos == std::string::npos) cout << "Position of find_first_of std::string::npos " << endl; else cout << "Position of find_first_of is " << pos << endl; °á°ú) Position of find is 2 ¹®ÀÚ¿ ÅäÅ«À¸·Î ºÐ¸®Çϱâ1ÇÔ¼ö »ç¿ë¹ý:void split(const std::string& str, Container& cont, const std::string& delims = " ") str : ÆĽÌÇÒ ¹®ÀÚ¿ cont : ÆÄ½ÌµÈ ¹®ÀÚ¿ ¸®½ºÆ®·Î std::vector ÀڷᱸÁ¶ÀÌ´Ù. delims : ÅäÅ« ¹®ÀÚ¿ÀÌ´Ù. ´ÙÀ½°ú °°ÀÌ ÅäÅ«À» ¿©·¯°³ ÁÙ¼öµµ ÀÖ´Ù. "#* " std::string text = "Let*#me*#split*#this into words"; std::vector<std::string> words; split(text, words, "#* ") Âü°í¼Ò½º) http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html #include <string>
#include <iostream> #include <vector> template <class Container> void split(const std::string& str, Container& cont, const std::string& delims = " ") { std::size_t current, previous = 0; current = str.find_first_of(delims); while (current != std::string::npos) { if(current != previous) cont.push_back(str.substr(previous, current - previous)); previous = current + 1; current = str.find_first_of(delims, previous); } if (previous < str.length()) { cont.push_back(str.substr(previous, current - previous)); } } int main() { std::string text = "Let*#me*#split*#this into words"; std::vector<std::string> words; split(text, words, "#* "); for (const auto& item : words) { std::cout << item << std::endl; } } °á°ú) Let me split this into words ¹®ÀÚ¿ ÅäÅ«À¸·Î ºÐ¸®Çϱâ2Âü°í¼Ò½º)http://vallista.tistory.com/entry/c-¹®ÀÚ¿-split-string-tokenizer ¼Ò½º¸¦ ¾à°£ ¼öÁ¤ÇÏ¿´´Ù. #include <string>
#include <vector> #include <iostream> using namespace std; std::vector<string> StringSplit(string strTarget, string strTok) { int nCutPos; int nIndex = 0; std::vector<string> strResult; while ((nCutPos = strTarget.find_first_of(strTok)) != strTarget.npos) { if (nCutPos > 0) { strResult.push_back(strTarget.substr(0, nCutPos)); } strTarget = strTarget.substr(nCutPos + 1); } if (strTarget.length() > 0) { strResult.push_back(strTarget.substr(0, nCutPos)); } return strResult; } void main() { std::string text = "Let*#me*#split*#this into words"; std::vector<std::string> words = StringSplit(text, "*# "); for (const auto& item : words) { std::cout << item << std::endl; } } ´Ù¸¥ ¹®ÀÚ¿ ºÐ¸® ¼Ò½º : string_split.cpp Âü°í) https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/ https://qiita.com/iseki-masaya/items/70b4ee6e0877d12dafa8 http://www.techiedelight.com/split-string-cpp-using-delimiter/ find_first_of, find ¼³¸í http://gakari.tistory.com/entry/CC-STL-¶óÀ̺귯¸®-string-ÇÔ¼ö-»ç¿ë¹ý |