Is it possible to use std::string in a constexpr?

As of C++20, yes, but only if the std::string is destroyed by the end of constant evaluation. So while your example will still not compile, something like this will: constexpr std::size_t n = std::string(“hello, world”).size(); However, as of C++17, you can use string_view: constexpr std::string_view sv = “hello, world”; A string_view is a string-like object … Read more

convert a char* to std::string

std::string has a constructor for this: const char *s = “Hello, World!”; std::string str(s); Note that this construct deep copies the character list at s and s should not be nullptr, or else behavior is undefined.

std::string formatting like sprintf

Modern C++ makes this super simple. C++20 C++20 introduces std::format, which allows you to do exactly that. It uses replacement fields similar to those in python: #include <iostream> #include <format> int main() { std::cout << std::format(“Hello {}!\n”, “world”); } Code from cppreference.com, CC BY-SA and GFDL Check out the compiler support page to see if it’s … Read more

How to concatenate a std::string and an int

In alphabetical order: std::string name = “John”; int age = 21; std::string result; // 1. with Boost result = name + boost::lexical_cast<std::string>(age); // 2. with C++11 result = name + std::to_string(age); // 3. with FastFormat.Format fastformat::fmt(result, “{0}{1}”, name, age); // 4. with FastFormat.Write fastformat::write(result, name, age); // 5. with the {fmt} library result = fmt::format(“{}{}”, … Read more

How to trim a std::string?

EDIT Since c++17, some parts of the standard library were removed. Fortunately, starting with c++11, we have lambdas which are a superior solution. #include <algorithm> #include <cctype> #include <locale> // trim from start (in place) static inline void ltrim(std::string &s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); } // trim from … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)