Skip to main content
Filter by
Sorted by
Tagged with
2 votes
2 answers
92 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 ...
glades's user avatar
  • 4,660
1 vote
1 answer
98 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 ...
MikyMax's user avatar
  • 11
1 vote
1 answer
121 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 ...
Involute's user avatar
  • 245
2 votes
1 answer
87 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, ...
WhiteKSI's user avatar
0 votes
1 answer
99 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<...
brandon xiong's user avatar
1 vote
1 answer
125 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?
SpeakX's user avatar
  • 385
2 votes
1 answer
94 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, ...
cvaqabond's user avatar
0 votes
1 answer
69 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 ...
Nils Rasmussen's user avatar
0 votes
0 answers
76 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::...
nofe1248's user avatar
-3 votes
2 answers
138 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; ...
Zenz's user avatar
  • 1
-2 votes
3 answers
131 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 <...
avent's user avatar
  • 27
2 votes
1 answer
148 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:...
Pietro's user avatar
  • 13.1k
1 vote
2 answers
167 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 ...
digito_evo's user avatar
  • 3,652
1 vote
1 answer
113 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 ...
Slava's user avatar
  • 44.2k
-1 votes
2 answers
47 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> ...
Fu11Stacks's user avatar
-1 votes
1 answer
80 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 ...
tadm123's user avatar
  • 8,766
0 votes
0 answers
54 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 ...
Santosh Sahu's user avatar
  • 2,244
0 votes
1 answer
92 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::...
ethqnol's user avatar
-3 votes
1 answer
142 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 ...
Tim's user avatar
  • 53
2 votes
1 answer
164 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 ...
Atmo's user avatar
  • 3,932
0 votes
2 answers
148 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 ...
Francis Cugler's user avatar
-1 votes
1 answer
81 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, ...
Carrot13's user avatar
3 votes
1 answer
174 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 ...
998244353's user avatar
0 votes
1 answer
69 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"); ...
Argus Kos's user avatar
  • 185
1 vote
2 answers
522 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 ...
kto's user avatar
  • 13
7 votes
1 answer
255 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> ...
Jason.Pu's user avatar
  • 131
0 votes
2 answers
94 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 ...
markt1964's user avatar
  • 2,826
5 votes
1 answer
273 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 ...
Dmitry Kuzminov's user avatar
0 votes
2 answers
265 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_; }...
morpheus's user avatar
  • 20.2k
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>::...
Regus Pregus's user avatar
2 votes
1 answer
244 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"] = "类型"; ...
keen's user avatar
  • 83
5 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::...
Yves's user avatar
  • 12.2k
54 votes
1 answer
5k 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 <...
Jan Schultke's user avatar
  • 38.3k
1 vote
1 answer
109 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::...
Santilín's user avatar
3 votes
2 answers
390 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-...
vengy's user avatar
  • 2,205
2 votes
1 answer
328 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 ...
malloy's user avatar
  • 23
4 votes
1 answer
624 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, ...
av4625's user avatar
  • 353
0 votes
3 answers
174 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++) ...
Maxou's user avatar
  • 47
2 votes
0 answers
182 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{ ...
Fedor's user avatar
  • 20.7k
0 votes
1 answer
84 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 ...
GaussGun's user avatar
  • 110
0 votes
2 answers
140 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 ...
Jerry's user avatar
  • 17
0 votes
1 answer
94 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 ...
user3443063's user avatar
  • 1,595
2 votes
1 answer
152 views

How many temporary std::string object be created in the code? (Effective Modern C++ materials)

I have been studying Effective Modern C++11 and 14 recently. I went through a part of the book located on page 171, item 25, where the author provides two code examples for the setName function. (1) //...
Peter's user avatar
  • 95
5 votes
2 answers
2k views

How to construct a `std::string` from a buffer that may or may not contain a null?

I have a char buffer of known size (for simplicity, let's say a fixed-sized array) from which I would like to construct a std::string. The string in this buffer may be null-terminated or it may run up ...
Parker Coates's user avatar
2 votes
3 answers
490 views

What is the most efficent way to convert a QStringView to a std::string?

I have a function that gets an argument as QStringView. Among other things, this must be converted into a std::string. QString has the function toStdString() for this. Is there a way to avoid the ...
Benjamin Buch's user avatar
1 vote
2 answers
634 views

How can I release or move std::string::c_str()?

I have a function which interfaces with other code that creates an object from a C-string. It goes roughly like this to build up the string before creating the object: ...
JM0's user avatar
  • 437
1 vote
1 answer
99 views

Is this temporary std::string expression acceptable?

I realize this isn't the most efficient thing to do, but is it acceptable C++ by most to create temporary std::string objects for concatenation, like here in line 4? constexpr const char* const a = &...
jemelter's user avatar
2 votes
2 answers
136 views

C++ std::string capitalize in non-latin language (without third-party libraries)

Considering the method: void Capitalize(std::string &s) { bool shouldCapitalize = true; for(size_t i = 0; i < s.size(); i++) { if (iswalpha(s[i]) && ...
Richard Topchii's user avatar
2 votes
2 answers
1k views

What is the correct way to “clear" a std::string_view?

I see this post: c++ - Why doesn't std::string_view have assign() and clear() methods? - Stack Overflow, so string_view does not contain clear function. But in my case, I have a string_view as a class ...
ollydbg23's user avatar
  • 1,190
0 votes
0 answers
106 views

Thread safety of std::string::c_str()

It appears to me as if there is a requirement in modern C++ that after calling operator[](), c_str(), or data(), it is guaranteed that string's contents are terminated by a null. However, is it ...
markt1964's user avatar
  • 2,826

1
2 3 4 5
25