1,216 questions
5
votes
0
answers
145
views
Short string processing in const expressions of Visual C++
I observe rather strange behavior of Visual Studio's compiler with short strings during constant evaluation, as can be demonstrated by this example:
#include <string>
constexpr char f( const ...
2
votes
1
answer
154
views
Appending formatted content to a `std::string` without creating temporaries
I want to create the following string: "name_1,name_2,...,name_100".
This can be accomplished using the following code:
#include <format>
#include <string>
// ...
const auto ...
3
votes
0
answers
148
views
C++ std::ssize works in one file but not another
I seem to run into issue with the use of std::ssize in C++.
I am able to compile in string.cpp but not string_quiz.cpp.
I ran this code with g++ -o string_quiz -std=c++23 string_quiz.cpp
string_quiz....
3
votes
1
answer
132
views
Do objects part of a class not get constructed until its constructor is called
For this simple classes below. For MyClass, the constructor initializes str with s during construction using the intializer list.
Does this mean that str doesn't get constructed at all yet until mc ...
0
votes
2
answers
285
views
How to resize a std::string and have it have the same capacity?
If I have an empty string and I do .resize() I expected the size and the capacity to be that size. That is not the case in GCC or Clang or MSVC. Requesting a size of 17 gives you a capacity of 30 or ...
1
vote
0
answers
56
views
std::string constructor not creating string from iterators [duplicate]
I'm trying to convert an std::array of integers to a string for output, so I tried using this string constructor:
template< class InputIt > basic_string( InputIt first, InputIt last, const ...
8
votes
0
answers
263
views
std::string comparison not constexpr in clang?
I want a templated function that calls a method of its template class depending on which class it is. I understand that a better way to achieve this would be using std::is_same, but irrelevant for the ...
2
votes
2
answers
120
views
snprintf to pre-sized std::string doesn't work? [duplicate]
I'm a bit lost why Option 1 doesn't work. In my understanding I should be able to write to a presized std::string whatever I want as long as I don't overshoot the string size, but it doesn't work on ...
1
vote
1
answer
138
views
Optimise C++ const std::string literal declaration for compile time initialization and runtime performance
I wish to optimise the use of const string literals in a bulky c++ 20 project to enhance compilation and runtime performance
In lots of source files string literals are declared with const std::string ...
22
votes
2
answers
2k
views
Why can I define a std::string instance that is constinit? Isn't constinit forbidden if an object requires dynamic initialization?
The cppreference mentions that if initialization contains a dynamic initialization part, the program is ill-formed when using constinit in C++20.
I just wonder why the following code compiles then:
...
1
vote
1
answer
148
views
How to define a generalisation to_string and to_wstring to avoid code duplication
I have a number of user-defined classes for which I would like to define a to_string function. However, instead of defining this for just std::string, I would like to define this for all the possible ...
2
votes
1
answer
105
views
String unshuffler returning different results on windows and linux
I'm working on a project that requires shuffling a string then unshuffling said string later.
Currently I shuffle the string on a Linux machine and attempt to unshuffle on a Windows machine, ...
0
votes
1
answer
115
views
Program Database Toolkit(PDT) cxxparse/cxxparse4101 error while parsering a simple c++ file contains both "#include<map>" and "std::to_string"
I came accros an error while using cxxparse/cxxparse4101 to parser a simple c++ source file. Can anyone give me a help?
c++ file:
//map.cpp
#include<iostream>
#include<map>
#include<...
1
vote
1
answer
152
views
std::string from const char* with zero allocation
When declaring std::string cpp{}; does this call new/malloc?
Assume we already have a const char* c. Is it possible to move the contents from c to cpp without extra allocations?
2
votes
1
answer
128
views
Why does std::string get corrupted when called from within a class?
I wanted to consult you about a problem I can't understand. I have a code similar to this:
#include "StdAfx.h"
#include "LocaleText.h"
PyObject* GetLocaleText(PyObject* poSelf, ...
0
votes
1
answer
77
views
char* vs std::string.data()
I'm using a Azure IoT Hub to send and receive messages to and from a device. To do this, I'm utilizing a library written in C and my application is written in C++. When I convert my std::string to a ...
0
votes
0
answers
103
views
Does this behavior of std::string conform to the standard? [duplicate]
When I compile the following code with -std=c++26
#include <string>
class Foo {
public:
constexpr static auto foo = std::string{"this_is_ok"};
constexpr static auto bar = std::...
-3
votes
2
answers
151
views
segmentation fault in c++ programme [duplicate]
output
the below code is a c++ program that calculates the sum of the letters in given text.
#include <iostream>
using namespace std;
int letter_sum(string text)
{
int Psum = 0;
int n;
...
-2
votes
3
answers
140
views
Getting random symbols after trying to replace chars in C++
I'm trying to replace every letter in string to next letter (a -> b, c -> d, etc.).
I get random symbols or XXXX after compiling and running the program.
#include <iostream>
#include <...
2
votes
1
answer
181
views
Can I pass a structure to `std::format`? [duplicate]
I have this structure which I need to pass to std::format:
#include <format>
#include <iostream>
#include <sstream>
#include <string>
struct Str {
int i;
float f;
std:...
1
vote
2
answers
221
views
Making a custom type behave like std::string
I have to define a few structs that contain a member of type std::string. I need to make them behave like std::string. In other words, they should be trivially convertible to std::string but not to ...
1
vote
1
answer
148
views
Is data returned by std::string::c_str() still valid after the string is moved?
Let's say I have a function that sends const char * data asynchronously, and invokes a callback when data is sent or an error happens (I omitted error handling as irrelevant to the question):
void ...
-1
votes
2
answers
69
views
How do I make my terminal quit on an empty cin input in c++?
I can't find a way to make my terminal quit the program on an empty input. I have:
int main(int argc, char const* argv[]) {
// Write your code here
// Define variables
set<string> ...
-1
votes
1
answer
94
views
How to convert a string to map to its respective enum type integer?
I'm trying to create a program that will ask the user to enter an animal name and will print its respective age.
This is what I have so far, I'm using enum types and std::arrays to map easily it's ...
0
votes
0
answers
57
views
C++ std::string initialization using new operator function [duplicate]
Have a basic snippet of initialization a std::string variable, and track the memory usage of program using overloaded new operator function as below:
static uint32_t s_AllocCount = 0;
// Overload of ...
0
votes
1
answer
98
views
C++ Iterators doing weird things
I might be completely stupid, but:
class Lexer {
private:
std::string::iterator start;
std::string::iterator current;
std::string source;
int line;
std::...
-3
votes
1
answer
169
views
How to iterate over std::string const& in a function?
I am passing a const reference std::string to a function. I want to iterate over the triples of the string and convert those triples to ints. I do so by bit shifting. My problem is that I do not know ...
2
votes
1
answer
177
views
Is std::basic_string a ReversibleContainer?
I was documenting a piece of code, writing that we need the chosen container to be a ReversibleContainer.Reading in details the description for std::vector and std::basic_string:
For std::vector and ...
0
votes
2
answers
195
views
Unexpected Outputs in a simple FizzBuzz program in C++
I was just trying my hand at implementing a simple FizzBuzz program where I'm trying to separate the ruleset from the logic and here is my attempt at doing this within Compiler Explorer, no use of any ...
-1
votes
1
answer
85
views
I do not understand this substring behavior . Rather trivial but it boggles my mind
string A = "LOLWUT";
cout << A.substr(0, A.length() - 1) << endl;
cout << A.substr(1, A.length() - 1) << endl;
This code prints:
LOLWU
OLWUT
From my understanding, ...
3
votes
1
answer
239
views
Why is dereference of past-the-end iterator of std::basic_string still UB after C++11?
As we all knows, C++11 adds a null terminator to std::basic_string (which doesn't count into most member functions). But when I read cpp ref, I found dereference of end() an UB (this paragraph is ...
0
votes
1
answer
78
views
Why std::locale doesn't work with std::u32string?
So I am trying to use std::tolower with custom locale.
It works with wstring, but with u32string I receive:
what(): std::bad_cast
This is what I do
auto loc = std::locale("el_GR.UTF-8");
...
1
vote
2
answers
792
views
How to store either std::string or std::string_view in a std::variant?
I am working on a lexer. I have a Token struct, which looks like this:
struct Token {
enum class Type { ... };
Type type;
std::string_view lexeme;
}
The Token's lexeme is just a view ...
7
votes
1
answer
299
views
why is std::copy faster than std::string constructor?
I tried these codes, to compare std::copy and std::string's constructor.
#include <chrono>
#include <iostream>
#include <vector>
void construct_test() {
std::vector<uint8_t> ...
0
votes
2
answers
102
views
split a string by a vector of strings
I have an input string, and I also have a vector of separators.
I want to output the vector of strings that are not in the separators and as an additional entry, tje separator that was found
So for ...
5
votes
1
answer
340
views
Why doesn't stoi allow std::basic_string as an input?
To increase the performance I'm trying to substitute std::string with custom specializations of std::basic_string where I'm replacing standard allocator with custom ones. One thing surprised me: the ...
0
votes
2
answers
291
views
Weird behavior with std::string reference class member
Given this code:
#include <iostream>
class Foo {
public:
Foo(const std::string& label) : label_(label) {}
void print() {
std::cout << label_;
}...
0
votes
0
answers
22
views
std::basic_string::iterator as a function argument [duplicate]
I am having difficulty understanding why the following piece of code does not compile (MSVC 2022, C++20 mode):
#include <string>
template <typename T>
void d1(std::basic_string<T>::...
2
votes
1
answer
325
views
How to store the Chinese strings correctly?
In Visual Studio 2022, using [tag:C++ 17]. I am trying to use a std::map to store Chinese strings:
std::map<std::string, std::string> translation;
translation["Type"] = "类型";
...
6
votes
3
answers
2k
views
Can I change a std::string, which has been assigned to a std::string_view
I just knew that C++17 introduced std::string_view. It doesn't hold any string, instead, it points to a string. If so, I'm confused by the case below:
std::string str = "abc";
std::...
55
votes
1
answer
6k
views
Why is initializing a string to "" more efficient than the default constructor?
Generally, the default constructor should be the fastest way of making an empty container.
That's why I was surprised to see that it's worse than initializing to an empty string literal:
#include <...
1
vote
1
answer
119
views
Can not create a wrapper around std::string that results in only "syntax sugar"
I know that std::string is not designed for inheritance, however, I wonder why this class definition doesn't compile:
using std::string;
class ExtendedString: public string
{
public:
using string::...
3
votes
2
answers
601
views
Rules for concatenating a CRLF (Carriage Return, Line Feed) to a std::string
This code below outputs
rm-data;
Content-Disposition: form-data;
https://godbolt.org/z/h5z89qv6f
#include <iostream>
#include <string>
int main()
{
std::string a = "Content-...
2
votes
1
answer
419
views
How to extract information from a stream in C++
I have a remote server that constantly sends a message like this to my pc:
{Heartbeat}
Furthermore, the remote server listens to my command messages that I send from my pc connected to the remote ...
5
votes
1
answer
998
views
Curly Brace Initialisation with std::string
I have been using curly brace initialisation more and more recently. Although I found a difference to round bracket initialisation in this case and I was wondering why.
If I do:
const std::string s(5, ...
0
votes
3
answers
198
views
C++ call to function by reference and copy
void ref(std::string& str)
{ (void)str; }
void copy(std::string str)
{ (void)str; }
int main()
{
std::string str = "Hello World";
for(size_t i = 0; i < 10000; i++)
...
2
votes
0
answers
194
views
Union with std::string inside in constant expression
I have some problems with aggregate initialization of a union containing std::string evaluated in constant expression. This example program demonstrates it:
constexpr bool f() {
union U{
...
0
votes
1
answer
94
views
SSO for strings stored in std::map
SSO enables short strings to be stored on stack. What if I have a std::map<std::string, std::string> (or any std container for that matter) which consists mainly of short strings (1 to 10 ...
0
votes
2
answers
154
views
Why is my string null while if I can access an individual char?
I am new to C++ and I recently learned pointers and references through u-demy lectures. After lectures, I am solving a problem to reverse a string using a separate function. I came up with a code but ...
0
votes
1
answer
115
views
How to read value of <std::vector<std::string>> *
I pass a std::vector<std::string>* to a function where I want to do something with each of the elements with type std::string. My code is the following but I get an error saying: terminate ...