stdstring

std::string::c_str() overwrittes the previous one returned by a function

不羁的心 提交于 2019-12-13 07:51:44
问题 I could not understand that how can be the pointers same when the text size is equal. It seems like firstStringObj::c_str() overwrittes the previous one's pointer. #include <iostream> #include <string> #include <string> #include <stdio.h> std::string getConstCharAndModifyItWithANewString( const char* constchar ) { std::string stringAtStack( constchar ); stringAtStack += "::isModified"; return stringAtStack; } int main() { const char* firstConstCharPointer =

What is the data type for a string? [closed]

旧街凉风 提交于 2019-12-13 04:43:06
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . This is most likely a very stupid question, but in C++, what is the data type for a string? I want to make the variable x = "Slim Shady". Would I declare

C++ comparing the index of a string to another string?

帅比萌擦擦* 提交于 2019-12-13 04:30:52
问题 How can I compare a single character from a string, and another string (which may or may not be greater than one character) This program gives me almost 300 lines of random errors. The errors don't reference a specific line number either, just a lot of stuff about "char* ", "", or "std::to_string". #include <iostream> #include <string> using std::cout; using std::string; int main() { string str = "MDCXIV"; string test = "D"; if (test == str[4]) { // This line causes the problems cout << test

Replace the character with two other

会有一股神秘感。 提交于 2019-12-13 03:59:35
问题 I have a std::string , how can i replace : character with %% ? std::replace( s.begin(), s.end(), ':', '%%' ); this code above doesn't work: error no instance matches the arguement list Thanks! 回答1: Unfortunately, there is no way to replace all : characters in one shot. But you can do it in a loop, like this: string s = "quick:brown:fox:jumps:over:the:lazy:dog"; int i = 0; for (;;) { i = s.find(":", i); if (i == string::npos) { break; } s.replace(i, 1, "%%"); } cout << s << endl; This program

String reverse, std::string does not allow character assignment?

末鹿安然 提交于 2019-12-13 03:59:30
问题 This is my code to reverse a string using std::string . But it does not work.. #include <string> #include <iostream> using namespace std; main() { string input; int i, j; cout << "Enter a string: "; getline(cin,input); string output; for(i = 0, j = input.length() - 1; i < input.length(); i++, j--) output[i]=input[j]; cout << "Reversed string = " << output; cin.get(); } But if we replace string output as char output[100]; it works. So std::string does not allow character assignments? 回答1: You

Why std::string does not have (explicit) const char* cast

青春壹個敷衍的年華 提交于 2019-12-13 02:55:30
问题 I like to know pro's and con's for having and not-having such cast. At several places including here on Stack Overflow I can see that the const char* cast is considered bad idea but I am not sure why? Lack of the (const char*) and forcing to always use c_str() cast creates some problems when writing generic routines and templates. void CheckStr(const char* s) { } int main() { std::string s = "Hello World!"; // all below will not compile with // Error: No suitable conversion function from "std

Split the string on dot and retrieve each values from it in C++

房东的猫 提交于 2019-12-12 20:34:10
问题 I need to split the string on . in C++.. Below is my string - @event.hello.dc1 Now I need to split on . on the above string and retrieve the @event from it and then pass @event to the below method - bool upsert(const char* key); Below is the code I have got so far after reading it from here - void splitString() { string sentence = "@event.hello.dc1"; istringstream iss(sentence); copy(istream_iterator<string>(iss), istream_iterator<string>(), ostream_iterator<string>(cout, "\n")); } But I am

Strange Error in using template<class InputIterator> string (InputIterator begin, InputIterator end);

Deadly 提交于 2019-12-12 13:35:19
问题 Given such a code segment: #include <iostream> #include <iterator> #include <fstream> #include <string> using namespace std; int main(){ ifstream file("1.txt"); string str((istream_iterator<char>(file)),istream_iterator<char>()); file.close(); cout<<str<<endl; } The code constructs a string from a file using istream_iterator. Notice that the first parameter of string constructor is enclosed with a pair of parentheses. If I omit the parentheses, there will be an error. In VC++ 2008, a link

string allocation in C++: why does this work? [duplicate]

江枫思渺然 提交于 2019-12-12 10:33:59
问题 This question already has answers here : Can a local variable's memory be accessed outside its scope? (20 answers) Closed 3 years ago . void changeString(const char* &s){ std::string str(s); str.replace(0, 5, "Howdy"); s = str.c_str(); } int main() { const char *s = "Hello, world!"; changeString(s); std::cout << s << "\n"; return 0; } When I run this code, it prints "Howdy, world!" I would think that str gets destroyed when changeString exits. Am I missing something with the way std::string

Calling std::~basic_string() in gdb

别说谁变了你拦得住时间么 提交于 2019-12-12 09:58:08
问题 As per @EvanED in https://stackoverflow.com/a/11311786/890753 I created a gdb command newstr to create a new std::string and put it in a gdb convenience variable: define newstr set ($arg0)=(std::string*)malloc(sizeof(std::string)) call ($arg0)->basic_string() # 'assign' returns *this; casting return to void avoids printing of the struct. call (void)( ($arg0)->assign($arg1) ) end It works great: (gdb) newstr $foo "hello world" (gdb) p $foo->c_str() $57 = 0xb22e388 "hello world" I use newstr in