Outils pour utilisateurs

Outils du site


lang:cpp:string

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
lang:cpp:string [2019/02/11 00:33] – Ajout de "wstring" rootlang:cpp:string [2023/05/29 11:58] (Version actuelle) – [consteval avec std::string_view] : fix lien root
Ligne 14: Ligne 14:
 </code> </code>
  
-[[https://cloud.tencent.com/developer/ask/99437|如何使用C ++编写std :: wstring文件的多平台方式?]] {{ :lang:cpp:string:如何使用c_编写std_wstring文件的多平台方式_-_问答_-_云_社区_-_腾讯云.mhtml |Archive du 10/02/2019}}+[[https://stackoverflow.com/questions/37395399/multiplatform-way-to-write-a-stdwstring-into-a-file-in-c|Multiplatform way to write a std::wstring into a file in C++]] {{ :lang:cpp:string:encoding_-_multiplatform_way_to_write_a_std_wstring_into_a_file_in_c_-_stack_overflow_2019-12-27_20_18_07_.html |Archive du 23/05/2016 le 27/12/2019}}
  
 +  * Conversion entre string et wstring
 +
 +Sous windows :
 +
 +<code cpp>
 +std::wstring s2ws(const std::string& s)
 +{
 +  int slength = (int)s.length() + 1;
 +  int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
 +  std::wstring r(len, L'\0');
 +  MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
 +  return r;
 +}
 +
 +std::string ws2s(const std::wstring& s)
 +{
 +  int slength = (int)s.length() + 1;
 +  int len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0); 
 +  std::string r(len, '\0');
 +  WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, &r[0], len, 0, 0); 
 +  return r;
 +}
 +</code>
 +
 +[[https://codereview.stackexchange.com/questions/419/converting-between-stdwstring-and-stdstring|Converting between std::wstring and std::string]] {{ :lang:cpp:string:c_-_converting_between_std_wstring_and_std_string_-_code_review_stack_exchange_04_05_2023_14_44_33_.html |Archive du 29/01/2011 le 04/05/2023}}
 +
 +====stringstream===
 +Le résultat ne va pas être le même si on donne à ''std::stringstream'' un ''int'' ou un ''char''.
 +
 +  ss << static_cast<char>(1);
 +
 +va donner ''\x1''.
 +
 +  ss << static_cast<int>(1);
 +
 +va donner ''1''.
 +
 +====consteval avec std::string_view====
 +
 +Ce n'est pas possible. Il faut travailler avec les ''char*''.
 +
 +<code cpp>
 +#include <array>
 +#include <cstring>
 +
 +template <class T, std::size_t N>
 +struct decayable_array : std::array<T, N> {
 +  constexpr operator const T*() const { return this->data(); }
 +};
 +
 +template <std::size_t N>
 +consteval decayable_array<char, N> reverse(const char (&arr)[N]) {
 +  decayable_array<char, N> data{};
 +
 +  for (std::size_t i = 0; i < N - 1; ++i) {
 +    data[N - i - 2] = arr[i];
 +  }
 +
 +  return data;
 +}
 +
 +int main() { return strlen(reverse("abc")); }
 +</code>
 +
 +<code asm>
 +main:
 +  mov     eax, 3
 +  ret
 +</code>
 +
 +[[https://godbolt.org/z/b4sY41Gbx|Exemple godbolt]]
 ====Split==== ====Split====
 +===string===
 <code cpp> <code cpp>
 std::istringstream f("denmark;sweden;india;us"); std::istringstream f("denmark;sweden;india;us");
Ligne 25: Ligne 97:
 </code> </code>
  
-[[https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g|Splitting a C++ std::string using tokens, e.g. “;”]] {{ :lang:cpp:string:splitting_a_c_std_string_using_tokens_e.g._-_stack_overflow.mhtml |Archive du 10/02/2019}}+[[https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g|Splitting a C++ std::string using tokens, e.g. “;”]] {{ :lang:cpp:string:splitting_a_c_std_string_using_tokens_e.g._-_stack_overflow_2019-12-27_20_18_56_.html |Archive du 02/03/2011 le 27/12/2019}} 
 + 
 +===string_view=== 
 +<code cpp> 
 +std::vector<std::string_view> 
 +splitSV(std::string_view strv, std::string_view delims = " ") 
 +
 +  std::vector<std::string_view> output; 
 +  size_t first = 0; 
 + 
 +  while (first < strv.size()) 
 +  { 
 +    const auto second = strv.find_first_of(delims, first); 
 + 
 +    if (first != second) 
 +      output.emplace_back(strv.substr(first, second-first)); 
 + 
 +    if (second == std::string_view::npos) 
 +      break; 
 + 
 +    first = second + 1; 
 +  } 
 + 
 +  return output; 
 +
 +</code> 
 + 
 +[[https://www.bfilipek.com/2018/07/string-view-perf-followup.html|Speeding Up string_view String Split Implementation]] {{ :lang:cpp:string:bartek_s_coding_blog_speeding_up_string_view_string_split_implementation_2019-12-27_20_19_05_.html |Archive du 30/07/2018 le 27/12/2019}}
lang/cpp/string.1549841593.txt.gz · Dernière modification : 2019/02/11 00:33 de root