Questions tagged [stdstring]
std::string is the C++ standard library's byte-based "string" type, defined in the <string> header.
1,102
questions
-2
votes
0
answers
40
views
How to find map value present in the string c++ here need to match map value to substring of a string
Below are map variables in code:
boardSelectMap.insert(std::pair<std::string, std::string>("boardtype1", "RBCM_CLEO"));
boardSelectMap.insert(std::pair<std::string, std::...
1
vote
0
answers
45
views
Does std::string use the malloc (free, realloc etc) family of functions to allocate data?
Consider this implementation of a custom string class append operator:
inline string& operator+=(const string& rhs){
if(rhs.size+(size-1)>capacity) {
data = ...
0
votes
1
answer
43
views
Caught std::exception, what(): basic_string::copy: __pos (which is 18446744073709551615) > this->size() (which is 3)
I recently try to check a substring of another string. The test thought resulting throwing an exception I couldn't quite understand:
#include <string>
#include <iostream>
bool solution(...
1
vote
1
answer
39
views
How to mix formatting in a single call using libfmt
I want to print the time center aligned. But I do not know how to mix arguments with fmt.
std::cout << fmt::format("|{0: ^80}|\n", "");
std::cout << fmt::format("|{...
2
votes
1
answer
61
views
Generating compile time functions string for formatting strings with libfmt
I want to create a nice table in stdout. The table has a lot of headers that are mainly compiletime strings. For example:
std::cout << fmt::format("|{0:-^80}|\n", "File ...
0
votes
2
answers
60
views
Replace every occurrence with double in string
I'm trying to write a function whose first parameter is a string and the second parameter is vector of real numbers. The function should return as a result a new string in which each occurrence ...
0
votes
1
answer
39
views
Make dynamically allocated object type string
I need to make dynamically allocated object type of string to store sentences and after that sentences should be sorted in alphabetical order using std::sort.
This would be correct solution using char ...
0
votes
3
answers
79
views
Save integers from string to vector
I need to save integers from string to vector.
Definition of number: each of strings substrings which consists entirely of digits, with a space before the first character of that substring and a space ...
0
votes
1
answer
14
views
Caveats reinterpret_cast'ing char* to unsigned char*?
I'm trying to fit my C++ app to a C-API. The API in question is mbed_tls which contains a base64 decoder:
int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *...
-2
votes
2
answers
58
views
How can you change the value of a string pointer that is passed to a function in C++?
I need to change the value of a std::string using a function.
The function must be void, and the parameter must be a pointer to a string as shown.
#include <iostream>
void changeToBanana(std::...
0
votes
2
answers
59
views
Does std::string needs explicit resizing or does it handles resizing itself?
I am trying to write a std::string in a file and then reading it back.
Why do i need to resize the string while reading back the text (see the commented line below while reading back the string)? ...
-2
votes
1
answer
70
views
Reading from file without using string
I am doing a school project where we must not use std::string. How can I do this? In the txt file the data are separated with a ";", and we do not know the length of the words.
Example:
...
0
votes
2
answers
75
views
C++ std::string attribute of a class comes up as an empty string after initialized
So i have a pretty straight foward homework that consist in creating a student class that has a name and 3 grades as attributes and a method to caluculate the final grade and append the name as long ...
0
votes
1
answer
52
views
Getting YAML::TypedBadConversion when trying to read string value with yaml-cpp
I have been having a error that I can't figure out why a getting it. When trying to read the following file example.yaml:
BFS_power:
graph: power.graph
type: METIS
BFS_avg:
graph: DEFAULT
type:...
0
votes
2
answers
60
views
Is there a use-case for std::string's operator= to not be lvalue ref-qualified?
The post here points out that std::string's member operator= is not lvalue ref-qualified. That allows us to write code such as this:
std::string() = "Hello";
The linked post asks why this ...
1
vote
2
answers
84
views
Can we create std::istringstream object from a reversed std::string
I am learning C++ using recommended C++ books. In particular, i read about std::istringstream and saw the following example:
std::string s("some string");
std::istringstream ss(s);
std::...
-4
votes
2
answers
100
views
Difference between string literal and string in cout statement?
What is the difference between the following ways of printing a string?
std::cout << "hello";
// and
std::cout << std::string("hello");
They both appear to do the ...
0
votes
1
answer
35
views
How to extract html tag name from std::string in C++?
In this example:
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include <sstream>
struct tag {
bool isOpening;
std::string name;
};
...
1
vote
0
answers
33
views
Correct usage of string_view and s suffix
I would like to evaluate the following code to understand the benefits of using string_view and s suffix.
std::string word{"apple"};
foo(word);
foo("banana");
foo("banana"...
0
votes
1
answer
83
views
How to assign a std::string to std::basic_string<unsigned short int, TRAITS_CLASS>(Unicode2String) on Linux
I am working on a Linux system, and I think standard Linux std::string supports both Unicode and ASCII characters. So, I want to use std::string in my code, but I receive strings from an application ...
3
votes
1
answer
250
views
Create std::string from std::span of unsigned char
I am using a C library which uses various fixed-sized unsigned char arrays with no null terminator as strings.
I've been converting them to std::string using the following function:
auto ...
0
votes
2
answers
117
views
custom exception class, using a base class, with multiple arguments
So I'm setting up custom exception classes for a program I am writing. I'm creating a catch-all base class, that I will use primarily as a generic exception. This base class will be inherited by ...
2
votes
0
answers
92
views
Paradox with default c'tor potentially throwing for a class with a std::string member
Do the C++ "Core Guidelines" prevent (or, at least, strongly discourage) a class with a std::string member from having a default constructor? I ask because of an issue I've noticed when ...
0
votes
1
answer
87
views
What are the downsides to accessing a std::map<std::string, int> using string literals?
I have written a ResourceManager class in C++ that contains a std::map<std::string, Resource>. My current code then accesses these methods using string literals, e.g:
// ResourceManager.h
class ...
-1
votes
1
answer
58
views
Run time error: "/home/keith/builds/mingw/gcc........" in VS Code while working with strings
This code is running properly in other online C++ compilers but is throwing an unexpected error in VS Code. Please point out the problem in my code. (The Error has also been attached below)
#include &...
3
votes
2
answers
59
views
Negating expression in if statement inside macro gives odd results
I've run into a somewhat strange issue. It makes me feel like the answer is blaringly obvious and I'm just not seeing something because the code is so simple.
I basically have a macro called "...
0
votes
0
answers
40
views
Use std::string as output-buffer for C-function [duplicate]
I need to use an old-style c-function that needs an output buffer. Instead of using a char-array for this and afterwards copy from this char-array to the std::string, I want to avoid the extra copy ...
0
votes
2
answers
73
views
How to work with Rcpp strings variables which could be NULL?
I am writing an R package + Rcpp code to work with an existing C++ library.
After going through the tutorials here: https://gallery.rcpp.org/articles/optional-null-function-arguments/ , I'm struggling ...
0
votes
0
answers
52
views
When does an std::string's _M_p become null?
While debugging a crash I noticed that _M_p of a string is null, but other properties seem to still contain remnants of a previous value.
Does anyone know which situation can cause _M_p to be null?
I ...
18
votes
1
answer
2k
views
constexpr std::string in C++20, how does it work?
Apparently, the constexpr std::string has not been added to libstdc++ of GCC yet (as of GCC v11.2).
This code:
#include <iostream>
#include <string>
int main( )
{
constexpr std::...
1
vote
1
answer
64
views
why doesn't assignment of int to std::map<string,string> produce a compiler error
Attempting to assign an int to a string
std::string s = 5;
produces the following compiler error:
error: conversion from ‘int’ to non-scalar type ‘std::string’ {aka ‘std::__cxx11::basic_string<char&...
0
votes
1
answer
117
views
Print elements of C++ string vector nicely in GDB
I want to view the content of std::vector<std::string> in GDB nicely
I can view it with just like in this suggestion
print *(myVector._M_impl._M_start)@myVector.size()
But it prints out all ...
0
votes
1
answer
281
views
string_view Vs const char* performance
Is a std::string_view parameter better than a const char* one in the code below?
void func( const std::string_view str )
{
std::istringstream iss( str.data( ) ); // str is passed to the ctor of ...
-1
votes
1
answer
67
views
Is changing the std::string value through it's address is valid?
I was wondering if we can modify std::string value through a pointer to it. Please consider the following example.
#include <iostream>
void func(std::string *ptr) {
*ptr = "...
1
vote
1
answer
65
views
What type would you use for passing file paths in your C++ library API?
Since I started using C++17 every time I have to develop a new library I consider several alternatives for passing file paths to public functions/methods. These are some of my thoughts I normally have ...
1
vote
2
answers
112
views
Create std::string from int8_t array
In some code int8_t[] type is used instead of char[].
int8_t title[256] = {'a', 'e', 'w', 's'};
std::string s(title); // compile error: no corresponding constructor
How to properly and safely create ...
1
vote
1
answer
57
views
move ctor of std::string does not work properly?
Why the msg is not being modified after the call to std::move(msg)?
int main()
{
std::string msg( "Error!" );
std::cout << "before try-catch: " << msg << ...
0
votes
2
answers
117
views
How can I return string as char* from a function in cpp?
for instance,
if str = "ab" is passed in a function returnString()
and we have a function defination like
string returnString(string str)
{
str+='c';
return str;
}
Output :- abc
...
5
votes
2
answers
400
views
How to write a custom exception class derived from std::invalid_argument?
How should I write an efficient exception class to show the error that can be prevented by fixing the source code mistakes before run-time?
This is the reason I chose std::invalid_argument.
My ...
0
votes
0
answers
45
views
A simple and reliable C++ benchmarking solution? [duplicate]
I want to know which of these two blocks runs faster:
std::string tempMsg( 13000, '\0' ); // constructs the string with a 13000 byte buffer from the start
tempMsg.clear( ); // clears those '\0' chars ...
-1
votes
1
answer
115
views
Are two heap allocations more expensive than a call to std::string fill ctor?
I want to have a string with a capacity of 131 chars (or bytes). I know two simple ways of achieving that. So which of these two code blocks is faster and more efficient?
std::string tempMsg( 131, '\0'...
0
votes
2
answers
39
views
How to hard code or declare the content of a file in std::string
I have the following content in a CSV file
Column1, Column2, Column3, Column4, Column5
something, false, somemore, 1.010000, 1.020000
Above CSV file content is returned as std::string from a method ...
0
votes
3
answers
172
views
C++: use std::string returned by a function: Using pointer to local variable that is out of scope
I've got the following function:
MyFunction(const char *value, bool trigger) {
if (trigger) {
std::string temporaryString = getTemporaryStringFromSomewhereElse();
value = ...
0
votes
3
answers
35
views
How to append more items to an existing vector contained in the value field of a std::map?
I have a std::vector<std::string>>. Following is my full program:
#include <iostream>
#include <vector>
#include <string>
#include <map>
int main() {
std::cout &...
0
votes
3
answers
148
views
How does cin read strings into an object of string class?
Reading some documentation online I found that istream class was part of C++ long before the string class was added. So the istream design recognizes basic C++ types such as double and int, but it is ...
0
votes
2
answers
79
views
String Rev function, strange behavior for out of bounds exception (c++)
I played with the string function,i wrote the following one, obviously I set the first character in the ret string to be written in a place that is out of bounds, but instead of an exception, I get a ...
0
votes
0
answers
48
views
std::to_string offset from beginning of the line for each row
I use std::to_string to print some numerical values(vectors, or matrices) in my terminal.
printer_func(to_string(m));
which will print:
[2011-12-22 11:34:30.441] [abcdefgho] [info] 0
0
0
1....
1
vote
1
answer
49
views
C++ - Error while usign arrays as parameter
I made a array of words and made a function to return a random word from the array. But it shows this error -
hangman.cpp: In function 'std::__cxx11::string get_random_word(std::__cxx11::string*)':
...
17
votes
2
answers
2k
views
C++20 constexpr vector and string not working
I'm getting a strange compiler error when trying to create constexpr std::string and std::vector objects:
#include <vector>
#include <string>
int main()
{
constexpr std::string cs{ &...
3
votes
4
answers
178
views
How to add several string together such as "123"+"456"?
How to achieve such operation, the Visual Studio always tells me that it was wrong.
The wrong code is C2110 and E2140.
Can anyone help?
std::string a = "2323" + "22323" + "...