stdstring

string and int concatenation in C++ [duplicate]

假装没事ソ 提交于 2021-02-17 06:41:06
问题 This question already has answers here : How to concatenate a std::string and an int? (23 answers) Closed 4 years ago . string words[5]; for (int i = 0; i < 5; ++i) { words[i] = "word" + i; } for (int i = 0; i < 5; ++i) { cout<<words[i]<<endl; } I expected result as : word1 . . word5 Bu it printed like this in console: word ord rd d Can someone tell me the reason for this. I am sure in java it will print as expected. 回答1: C++ is not Java. In C++, "word" + i is pointer arithmetic, it's not

How to use std::string in a QLineEdit?

别来无恙 提交于 2021-02-16 13:31:31
问题 I have the following problem. I am trying to integrate a large code written by me with a Qt interface. Some of my functions return std::string . I did not succeed in making QLineEdit::setText accept them (other functions returning char do not give me problems). What should I do? Thanks! Giuseppe 回答1: Try this: std::string a = "aaa"; lineEdit->setText(QString::fromStdString(a)); You will need Qt with STL support. 回答2: There's no constructor for QString that takes a std::string . Convert it

How to use std::string in a QLineEdit?

我怕爱的太早我们不能终老 提交于 2021-02-16 13:29:09
问题 I have the following problem. I am trying to integrate a large code written by me with a Qt interface. Some of my functions return std::string . I did not succeed in making QLineEdit::setText accept them (other functions returning char do not give me problems). What should I do? Thanks! Giuseppe 回答1: Try this: std::string a = "aaa"; lineEdit->setText(QString::fromStdString(a)); You will need Qt with STL support. 回答2: There's no constructor for QString that takes a std::string . Convert it

basic_regex throws bad_cast with char32_t

北城余情 提交于 2021-02-11 06:51:07
问题 Why does the following code generate std::bad_cast exception? #include <iostream> #include <regex> #include <string> int main() { std::basic_string<char32_t> reg = U"^\\w"; try { std::basic_regex<char32_t> tagRegex(reg); } catch(std::exception &e) { std::cout << e.what() << std::endl; } return 0; } This sample on Ideone for convenience: https://ideone.com/Saea88 Using char or wchar instead of char32_t runs without throwing though (proof: https://ideone.com/OBlXed). 回答1: You can find here:

Is it possible to use C++11 ABI _and_ both cxx11-style and old-style strings?

北城余情 提交于 2021-02-08 10:14:44
问题 I have some code which is being built with GCC 5.3.1 without _GLIBCXX_CXX11_ABI set. Now, suppose I want to use both old-style and new-style std::__cxx11::string 's in the same bit of code . Is that possible? If so, how? Notes: I don't really have a good reason for doing this. I do have a not-so-good reason, but let's make it a theoretical question please. It's ok if the C++11 strings aren't called std::string . 回答1: Can you have both the old and new string implementation in the same code?

EXC_BAD_ACCESS error for std::string member of a structure

一世执手 提交于 2021-02-08 09:39:13
问题 On accessing a struct member of type std::string, the error Bus Error: 10 popped up. Code is as following. #include <iostream> #include <string> struct KeyValuePair { std::string key; std::string value; }; struct KeyValuePair *temp = (struct KeyValuePair *) malloc(sizeof(struct KeyValuePair)); int main(void) { temp->value = "|"; temp->value += "someval|"; std::cout << temp->value << std::endl; return 0; } Running gdb on the code shows the following at the line temp->value = "|" . Program

is there a way to set the length of a std::string without modifying the buffer content?

最后都变了- 提交于 2021-02-07 12:36:33
问题 According to the statements made in the answers of these questions Is writing to &str[0] buffer (of a std:string) well-defined behaviour in C++11? Is it legal to write to std::string? writing directly to std::string internal buffers .. in C++11 it should be possible to call a C API function which takes a char pointer to store the output like this: str::string str; str.reserve(SOME_MAX_VALUE); some_C_API_func(&str[0]); But is there now a legal way to set the size of the string to the length of

Check if a string contains only the characters in another string

我们两清 提交于 2021-02-04 21:56:27
问题 I want to write a function that determines if all the letters of an inputted word are contained in another string of acceptable letters. bool ContainsOnly(std::string inputtedWord, std::string acceptableLetters) { // ... how do I write this? } Here's my testing framework: bool Tester(std::string inputtedWord, std::string acceptableLetters) { if (ContainsOnly(inputtedWord, acceptableLetters)) { std::cout << "Good!" << std::endl; return true; } else { std::cout << "No good!" << std::endl;

why cout is not printing this string?

蓝咒 提交于 2021-01-27 11:50:55
问题 So, I'm new to C++ and I can't figure why this is happening. I have a string with all the alphabets and I copied 10 characters from it into a new string s2 , character by character using a for loop as follows and when I execute it the cout function is printing a blank line. #include <iostream> using namespace std; int main(){ string s = "abcdefghijklmnopqrstuvwxyz"; string s2; for(int i=0; i<10; i++){ s2[i] = s[i]; } cout << s2 << endl; return 0; } But when I print this string s2 character by

why cout is not printing this string?

﹥>﹥吖頭↗ 提交于 2021-01-27 11:45:13
问题 So, I'm new to C++ and I can't figure why this is happening. I have a string with all the alphabets and I copied 10 characters from it into a new string s2 , character by character using a for loop as follows and when I execute it the cout function is printing a blank line. #include <iostream> using namespace std; int main(){ string s = "abcdefghijklmnopqrstuvwxyz"; string s2; for(int i=0; i<10; i++){ s2[i] = s[i]; } cout << s2 << endl; return 0; } But when I print this string s2 character by