All Questions
39 questions
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:
...
4
votes
1
answer
414
views
How to convert char* into std::u8string?
Intro
If I catch an exception, I want to convert the error message, which is returned as a C-style string by the what() method, into a std::u8string (a UTF-8 string). For example: std::u8string(error....
0
votes
1
answer
948
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 ...
0
votes
2
answers
2k
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
4k
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
1
answer
77
views
String fundamentals
So far I know the following ways to have strings in C++ (although some of these are from C)
string string1 = "Hello";
char string2[] = "Hello";
char string3[] = {'H', 'e', 'l', 'l',...
4
votes
1
answer
2k
views
Is there a std::string equivalent for CString::Mid()?
Is there any equivalent function in std::string for CString::mid()?
9
votes
1
answer
1k
views
Comparing std::string and C-style string literals
Suppose I have the following code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; // or std::
int main()
{
string s1{ "Apple" };
cout <<...
2
votes
1
answer
450
views
Can I delete function for an rvalue version of object?
For legacy reasons, there's a lot of usage of const char* in the code I'm working on. I am trying to limit that, and stumbled on something I'd like to know. I have something like:
class AClass {
...
-2
votes
2
answers
115
views
Using c-strings and std::string in the same code
I would like to know whether the following code is "valid":
#include <iostream>
using namespace std;
int main(void) {
string s="Hello World!\n";
for (int i=0;i<s.size();++i) {
...
1
vote
2
answers
1k
views
C++ creating CString from std::string fails
In my MFC based application this code:
std::string stdString("MappingName");
std::cout << "as String: " << stdString << std::endl;
CString cString(stdString.c_str());
std::cout <&...
0
votes
1
answer
346
views
Boost read_until docs
I'm pretty much a newbie to Boost in general and Boost Asio more specifically-- I've kind of been thrown into the deep end. I'm reading the Boost docs and I'm wondering if I've run into a typo in the ...
1
vote
1
answer
1k
views
Why does passing a std::string to CString.Format() only crash sometimes?
Using CString.Format(), I'm passing it a std::map that returns a std::string when given an int.
So:
CString cStr;
cStr.Format("%s", IntToStdStringMap[1]);
where IntToStdStringMap[1] returns some ...
1
vote
2
answers
391
views
How to use std::string effectively in the C-style functions which deals with conventional c-strings?
This is an old problem, which I have observed in past. So thought of getting a clarification once & for all. There are many standard / orthodox C library functions, which deal only with C-style ...
1
vote
2
answers
108
views
Why is my std::string obtained via stream being overwritten?
Assume I have a function like so:
std::string get_shader(std::string path) {
std::string fullpath = "./resources/shaders/" + path;
std::ifstream vertexShaderFile(fullpath);
std::...
3
votes
1
answer
1k
views
Passing the results of `std::string::c_str()` to `mkdtemp()` using `const_cast<char*>()`
OK, so: we all know that generally the use of const_cast<>() anywhere is so bad it’s practically a programming war crime. So this is a hypothetical question about how bad it might be, exactly, ...
11
votes
3
answers
1k
views
Default advice for using C-style string literals vs. constructing unnamed std::string objects?
So C++ 14 introduced a number of user-defined literals to use, one of which is the "s" literal suffix, for creating std::string objects. According to the documentation, its behavior is exactly the ...
1
vote
1
answer
1k
views
Convert string to CString
I have a string which is declared like this:
string query = "INSERT INTO tblVehicle (OVImage) VALUES ('";
query += decodedData;
query += "')";
(where 'decodedData' is a string holding Base64 data)
...
1
vote
2
answers
117
views
convert c-style-sting to std::string inside initialization list of a constructor
Is it safe to convert char* to std::string inside initialization list of a constructor like below:
class MyClass{
public:
MyClass(){}
MyClass(
char*char_Ptr_Definition_File
,char*...
0
votes
3
answers
537
views
Returning Char Pointer Without Heap
I was working on a program and I noticed something that didn't really make a lot of sense to me. std::string has a function called c_str() which returns a C-style string (NULL-terminated) ...
10
votes
3
answers
19k
views
How to elegantly initialize vector<char *> with string literal?
The problem comes from an exercise on C++ Primer 5th Edition:
Write a program to assign the elements from a list of char* pointers
to C-style character strings to a vector of strings.
-----------...
1
vote
2
answers
183
views
How to use random_shuffle with CString?
I would like to shuffle the characters present in CString varible. How do i do it?
Std provide a finction called random_shuffle() which can be used to shuffle std::string in the following way
std::...
0
votes
3
answers
7k
views
User input filename
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstring>
using namespace std;
const int WL = 20;
const int WR = 1000;
void READ (ifstream &, char[], ...
0
votes
1
answer
2k
views
Conversion of CString to std::string without 'GetString()'
if m_attr.GetValue() returns a CString then:
why does the below code work without error message ... only warning message 'C4927'?
std::string sigCncDialog::GetSignalAttributeValue()
{return m_attr....
0
votes
3
answers
208
views
C++ variable lifetime -- need workaround to return temporary
I have a C++ object (boost::format) that has a str() function which returns an std::string.
So when I need a formatted C string, I need to write something like:
(boost::format("%1% %2%") % "1" % "2")...
4
votes
2
answers
23k
views
C++: how to convert ASCII or ANSI to UTF8 and stores in std::string
My company use some code like this:
std::string(CT2CA(some_CString)).c_str()
which I believe it converts a Unicode string (whose type is CString)into ANSI encoding, and this string is for a ...
1
vote
1
answer
262
views
Compile time string assignment for run-time identification
I've been using a pattern in a library I'm creating that uses passes a String name of an object to its base object's constructor. I've tried using std::string and c-style strings but keep getting ...
0
votes
2
answers
490
views
How to construct a CString/std::string from a string position
Given the following:
for( std::string line; getline( input, line ); )
{
CString strFind = line.c_str();
int n = strFind.ReverseFind( '\\' );
CString s = CString( strFind,n );
...
4
votes
3
answers
35k
views
How to convert an std::string to C-style string [duplicate]
I am programming in C++.
As basic as this question is I cannot seem to find an answer for it anywhere. So here is the problem:
I want to create a C-style string however I want to put an integer ...
4
votes
1
answer
760
views
How to manage string slices with less overhead?
I'm dealing with giant (up to 2GB) strings and their slices in C++ program. C-style strings seem to be unreliable under such circumstances, but can be sliced trivially (without '\0' at the end). On ...
1
vote
7
answers
2k
views
std::string.c_str() has different value than std::string?
I have been working with C++ strings and trying to load char * strings into std::string by using C functions such as strcpy(). Since strcpy() takes char * as a parameter, I have to cast it which goes ...
2
votes
3
answers
1k
views
CString construction from std::string - copy chars or pointer
If I convert a std::string into a CString using something like:
std::string ss("Foo");
CString cs( ss.c_str() );
Does the CString copy the characters from ss or does it simply copy the char* pointer?...
25
votes
4
answers
25k
views
MFC: std::string vs CString?
Using C++ with MFC. Coming from a C# background I typically just use string for all, well, strings. I use them for class members, method parameters, and method return values.
Now in C++ I've got ...
4
votes
2
answers
401
views
Is std::string a better idea than char* when you're going to have to pass it as a char*?
In a recent question, I learned that there are situations where you just gotta pass a char* instead of a std::string. I really like string, and for situations where I just need to pass an immutable ...
2
votes
6
answers
1k
views
What is the most efficient way to convert STL string array to const char* array?
We have:
std::string string_array[2];
string_array[0] = "some data";
string_array[1] = "some more data";
char* cstring_array[2];
What is the most efficient way to copy data from string_array ...
0
votes
1
answer
783
views
unformatted input to a std::string instead of c-string from binary file
ok i have this program working using c-strings. I am wondering if it is possible to read in blocks of unformatted text to a std::string? I toyed arround with if >> but this reads in line by ...
52
votes
4
answers
28k
views
Can a std::string contain embedded nulls?
For regular C strings, a null character '\0' signifies the end of data.
What about std::string, can I have a string with embedded null characters?
4
votes
3
answers
5k
views
C++ difference between automatic type conversion to std::string and char*
As a learning exercise, I have been looking at how automatic type conversion works in C++. I know that automatic type conversion should generally be avoided, but I'd like to increase my knowledge of ...
94
votes
16
answers
273k
views
How do you convert CString and std::string std::wstring to each other?
CString is quite handy, while std::string is more compatible with STL container. I am using hash_map. However, hash_map does not support CStrings as keys, so I want to convert the CString into a std::...