Outils pour utilisateurs

Outils du site


lang:cpp:string

Ceci est une ancienne révision du document !


Table des matières

wstring

wstring est surtout utile pour Windows car toutes les appels de fonctions finissant par W en ont besoin.

#include <fstream>
#include <ios>
#include <codecvt>
 
std::wofstream out("file.txt", std::ios_base::out | std::ios_base::app);
const std::locale utf8_locale = std::locale(std::locale(), new std::codecvt_utf8<wchar_t>());
out.imbue(utf8_locale);
// Verify that the file opened correctly
std::wstring s(L"contenu");
out << s << std::endl;

如何使用C ++编写std :: wstring文件的多平台方式? Archive du 10/02/2019

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.

Split

string

std::istringstream f("denmark;sweden;india;us");
std::string s;    
while (getline(f, s, ';')) {
  // Résultat dans s.
}

Splitting a C++ std::string using tokens, e.g. “;” Archive du 10/02/2019

string_view

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;
}

Speeding Up string_view String Split Implementation Archive du 10/02/2019

lang/cpp/string.1557241557.txt.gz · Dernière modification : 2019/05/07 17:05 de root