Enviado em 05/05/2018 - 10:13h
No Java, as Strings, bytes, tem o metodo split(delimitador) que retorna um array do tipo criado com base num delimitador, queria sabe como faço o mesmo em C++Enviado em 06/05/2018 - 14:55h
Escrevendo, agora, da casa dos meus pais, onde tenho hospedados alguns alfarrábios./* split.h */ #ifndef __SPLIT_H__ #define __SPLIT_H__ #include <regex> #include <string> #include <vector> #define SPLIT_H_RCSHEADER "$Header: $" std::vector<std::string> split(const std::string &str, const std::string &sep, size_t max_fields=0); std::vector<std::string> split(const std::string &str, const std::regex &sep_re, size_t max_fields=0); std::vector<std::string> split(const std::string &str, size_t max_fields=0); #endif // !defined(__SPLIT_H__)
/* split.cc */ #include <regex> #include <string> #include <vector> #include "split.h" using namespace std; namespace { const string RCSHEADER{"$Header: $"}; const string HEADER_RCSHEADER{SPLIT_H_RCSHEADER}; } vector<string> split(const string &str, const string &sep, size_t max_fields){ vector<string> result; const size_t str_len=str.length(); if(str_len){ const size_t sep_len=sep.length(); const size_t empty_sep=!sep_len; size_t a, b; for( a=0; ( (max_fields==0 || result.size()<max_fields-1) && (b=str.find(sep, a))!=str.npos && a<str_len-sep_len ); a=b+sep_len ){ b+=empty_sep; result.emplace_back(str, a, b-a); } if((a<str.length() || (a==str.length() && result.size()<max_fields))) result.emplace_back(str, a); } return result; } vector<string> split(const string &str, const regex &sep_re, size_t max_fields){ vector<string> result; const size_t str_len=str.length(); if(str_len){ smatch sep; auto a=str.begin(); while( a!=str.end() && (max_fields==0 || result.size()<max_fields-1) && regex_search(a, str.end(), sep, sep_re) ){ result.emplace_back(a, a+sep.position(0)+!sep.length(0)); a+=sep.position(0)+sep.length(0)+!sep.length(0); } if((a!=str.end() || (a==str.end() && result.size()<max_fields))) result.emplace_back(a, str.end()); } return result; } vector<string> split(const string &str, size_t max_fields){ return split(str, regex("\\s+"), max_fields); }
/* test.cc */ #include <iostream> #include <regex> #include <vector> #include "split.h" using namespace std; string join(const vector<string> &v, const string &sep){ string result; if(v.size()){ auto v_i=v.cbegin(); result=*v_i; while(++v_i!=v.cend()) (result+=sep)+=*v_i; } return result; } void test(const string &descr, const vector<string> &v){ cout << " " << descr << " yields "; if(v.empty()) cout << "nothing.\n"; else cout << v.size() << " part(s): \"" << join(v, "\", \"") << "\".\n"; } int main(){ vector<string> test_texts{ "Isto e um teste.", "Isto e um\tteste.", "\t\t\t Isto e um teste. ", "\t \n \r \t", "" }; vector<string> parts; for(const auto &text: test_texts){ cout << "text=\"" << text << "\":\n"; test("split(text, \" \")", split(text, " ")); test("split(text, \" \", 3)", split(text, " ", 3)); test("split(text, regex(\" +\"))", split(text, regex(" +"))); test("split(text, regex(\" +\"), 3)", split(text, regex(" +"), 3)); test("split(text)", split(text)); test("split(text, 3)", split(text, 3)); cout << '\n'; } }
Enviado em 06/05/2018 - 01:10h
Embora C++ não possua uma função de split, existem vários idiomas que cumprem essa tarefa.#include <iostream> #include <sstream> #include <vector> std::vector<std::string> split(const std::string& text, const char delimiter = ' ') { std::vector<std::string> tokens; size_t current; size_t next = -1; do { current = next + 1; next = text.find_first_of(delimiter, current); tokens.push_back(text.substr(current, (next - current))); } while (next != std::string::npos); return tokens; } int main() { std::string s = "viva o linux"; const std::vector<std::string> tokens = split(s); for (const std::string& token : tokens) { std::cout << token << std::endl; } return 0; }
Enviado em 06/05/2018 - 07:09h
Como você usou apenas um caráter como terminador, poderia ter usado std:: string::find() em lugar de std::string::find_first_of(), ou então usar um conjunto de caracteres terminadores alternativos, colocando o segundo parâmetro em “const char *” ou “const std::string &”.Enviado em 07/05/2018 - 17:41h
Aqui tem um cardápio bem vasto de opções para você dividir sua string em C++:Criar entrada (menuentry) ISO no Grub
Como gerar qualquer emoji ou símbolo unicode a partir do seu teclado
Instalar o VIM 9.1 no Debian 12
Como saber o range de um IP público?
Muitas dificuldades ao instalar distro Linux em Notebook Sony Vaio PCG-6131L (VPCEA24FM)
Slackpkg+ (Slackpkg Plus) está de volta!
Como dividir duas janelas igualmente e lado-a-lado na sua tela
compilação samba 4.22 rock linux 9.5 (4)
Problemas com SQL em objeto TLabel ... [RESOLVIDO] (3)