Skip to main content
Filter by
Sorted by
Tagged with
5 votes
0 answers
145 views

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 ...
Fedor's user avatar
  • 24.8k
2 votes
1 answer
154 views

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 ...
PlsHelp's user avatar
  • 337
3 votes
0 answers
148 views

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....
dansyw91's user avatar
3 votes
1 answer
132 views

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 ...
Engineer999's user avatar
  • 4,159
0 votes
2 answers
285 views

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 ...
Zebrafish's user avatar
  • 16.5k
1 vote
0 answers
56 views

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 ...
venyanwarrior's user avatar
8 votes
0 answers
263 views

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 ...
Ivan Gorin's user avatar
2 votes
2 answers
120 views

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 ...
glades's user avatar
  • 5,392
1 vote
1 answer
138 views

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 ...
MikyMax's user avatar
  • 11
22 votes
2 answers
2k views

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: ...
Ijaz Ahmad's user avatar
1 vote
1 answer
148 views

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 ...
Involute's user avatar
  • 245
2 votes
1 answer
105 views

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, ...
WhiteKSI's user avatar
0 votes
1 answer
115 views

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<...
brandon xiong's user avatar
1 vote
1 answer
152 views

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?
SpeakX's user avatar
  • 427
2 votes
1 answer
128 views

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, ...
cvaqabond's user avatar
0 votes
1 answer
77 views

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 ...
Nils Rasmussen's user avatar
0 votes
0 answers
103 views

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::...
nofe1248's user avatar
-3 votes
2 answers
151 views

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; ...
Zenz's user avatar
  • 1
-2 votes
3 answers
140 views

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 <...
avent's user avatar
  • 27
2 votes
1 answer
181 views

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:...
Pietro's user avatar
  • 13.5k
1 vote
2 answers
221 views

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 ...
digito_evo's user avatar
  • 3,735
1 vote
1 answer
148 views

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 ...
Slava's user avatar
  • 44.5k
-1 votes
2 answers
69 views

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> ...
Fu11Stacks's user avatar
-1 votes
1 answer
94 views

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 ...
tadm123's user avatar
  • 8,899
0 votes
0 answers
57 views

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 ...
Santosh Sahu's user avatar
  • 2,244
0 votes
1 answer
98 views

I might be completely stupid, but: class Lexer { private: std::string::iterator start; std::string::iterator current; std::string source; int line; std::...
ethqnol's user avatar
-3 votes
1 answer
169 views

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 ...
Tim's user avatar
  • 53
2 votes
1 answer
177 views

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 ...
Atmo's user avatar
  • 4,272
0 votes
2 answers
195 views

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 ...
Francis Cugler's user avatar
-1 votes
1 answer
85 views

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, ...
Carrot13's user avatar
3 votes
1 answer
239 views

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 ...
998244353's user avatar
0 votes
1 answer
78 views

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"); ...
Argus Kos's user avatar
  • 185
1 vote
2 answers
792 views

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 ...
kto's user avatar
  • 13
7 votes
1 answer
299 views

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> ...
Jason.Pu's user avatar
  • 131
0 votes
2 answers
102 views

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 ...
markt1964's user avatar
  • 2,898
5 votes
1 answer
340 views

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 ...
Dmitry Kuzminov's user avatar
0 votes
2 answers
291 views

Given this code: #include <iostream> class Foo { public: Foo(const std::string& label) : label_(label) {} void print() { std::cout << label_; }...
morpheus's user avatar
  • 20.7k
0 votes
0 answers
22 views

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>::...
Regus Pregus's user avatar
2 votes
1 answer
325 views

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"] = "类型"; ...
keen's user avatar
  • 83
6 votes
3 answers
2k views

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::...
Yves's user avatar
  • 12.6k
55 votes
1 answer
6k views

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 <...
Jan Schultke's user avatar
1 vote
1 answer
119 views

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::...
Santilín's user avatar
3 votes
2 answers
601 views

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-...
vengy's user avatar
  • 2,467
2 votes
1 answer
419 views

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 ...
malloy's user avatar
  • 23
5 votes
1 answer
998 views

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, ...
av4625's user avatar
  • 373
0 votes
3 answers
198 views

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++) ...
Maxou's user avatar
  • 47
2 votes
0 answers
194 views

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{ ...
Fedor's user avatar
  • 24.8k
0 votes
1 answer
94 views

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 ...
GaussGun's user avatar
  • 120
0 votes
2 answers
154 views

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 ...
Jerry's user avatar
  • 17
0 votes
1 answer
115 views

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 ...
user3443063's user avatar
  • 1,697

1
2 3 4 5
25