stdstring

std::string::assign vs std::string::operator=

允我心安 提交于 2019-12-01 15:19:07
问题 I coded in Borland C++ ages ago, and now I'm trying to understand the "new"(to me) C+11 (I know, we're in 2015, there's a c+14 ... but I'm working on an C++11 project) Now I have several ways to assign a value to a string. #include <iostream> #include <string> int main () { std::string test1; std::string test2; test1 = "Hello World"; test2.assign("Hello again"); std::cout << test1 << std::endl << test2; return 0; } They both work. I learned from http://www.cplusplus.com/reference/string

The procedure entry point _ZNSt7_cxx1112basic_stringlcSt11char_traitslcESalcEEC1Ev could not be located in the dynamic link library

杀马特。学长 韩版系。学妹 提交于 2019-12-01 11:20:12
问题 I am having immense difficulty trying to use std::string. The program compiles absolutely fine, but when I run the program, I receive this error: error I have looked everywhere on google and haven't found a thing on how to solve this issue. I have also tried writing my code differently to see if that would tell me anything. std::string yourName; std::cout << "What is your name?"; std::cin >> yourName; std::cout << "Hello, " << yourName << std::endl; Despite the rewrite, I am still getting the

Can you specify what ISN'T a delimiter in std::getline?

家住魔仙堡 提交于 2019-12-01 08:46:10
I want it to consider anything that isn't an alphabet character to be a delimiter. How can I do this? You can't. The default delimiter is \n : while (std::getline (std::cin, str) // '\n' is implicit For other delimiters, pass them: while (std::getline (std::cin, str, ' ') // splits at a single whitespace However, the delimiter is of type char, thus you can only use one "split-character", but not what not to match. If your input already happens to be inside a container like std::string , you can use find_first_not_of or find_last_not_of . In your other question, are you sure you have considered

Can you specify what ISN'T a delimiter in std::getline?

混江龙づ霸主 提交于 2019-12-01 07:21:14
问题 I want it to consider anything that isn't an alphabet character to be a delimiter. How can I do this? 回答1: You can't. The default delimiter is \n : while (std::getline (std::cin, str) // '\n' is implicit For other delimiters, pass them: while (std::getline (std::cin, str, ' ') // splits at a single whitespace However, the delimiter is of type char, thus you can only use one "split-character", but not what not to match. If your input already happens to be inside a container like std::string ,

Avoiding improper std::string initialization with NULL const char* using g++

旧街凉风 提交于 2019-12-01 00:17:06
问题 A there any g++ options which can detect improper initialization of std::string with NULL const char*? I was in the process of turning some int fields into std::string ones, i.e: struct Foo { int id; Foo() : id(0) {} }; ...turned into: struct Foo { std::string id; Foo() : id(0) {} //oooops! }; I completely overlooked bad 'id' initialization with 0 and g++ gave me no warnings at all. This error was detected in the run time(std::string constructor threw an exception) but I'd really like to

Substring of a std::string in utf-8? C++11

帅比萌擦擦* 提交于 2019-11-30 20:30:17
I need to get a substring of the first N characters in a std::string assumed to be utf8. I learned the hard way that .substr does not work... as... expected. Reference: My strings probably look like this: mission:\n\n1億2千万匹 I found this code and am just about to try it out. std::string utf8_substr(const std::string& str, unsigned int start, unsigned int leng) { if (leng==0) { return ""; } unsigned int c, i, ix, q, min=std::string::npos, max=std::string::npos; for (q=0, i=0, ix=str.length(); i < ix; i++, q++) { if (q==start){ min=i; } if (q<=start+leng || leng==std::string::npos){ max=i; } c =

How to force std::stringstream operator >> to read an entire string?

末鹿安然 提交于 2019-11-30 20:12:09
How to force std::stringstream operator >> to read an entire string instead of stopping at the first whitespace? I've got a template class that stores a value read from a text file: template <typename T> class ValueContainer { protected: T m_value; public: /* ... */ virtual void fromString(std::string & str) { std::stringstream ss; ss << str; ss >> m_value; } /* ... */ }; I've tried setting/unsetting stream flags but it didn't help. Clarification The class is a container template with automatic conversion to/from type T. Strings are only one instance of the template, it must also support other

Append int to std::string

元气小坏坏 提交于 2019-11-30 07:46:13
问题 I tried two different ways to append an int to a std::string , and to my surprise, I got different results: #include <string> int main() { std::string s; s += 2; // compiles correctly s = s + 2; // compiler error return 0; } Why does it compile and work correctly when I use the += operator, but fail when I use the + operator? I don't think the question is like How to concatenate a std::string and an int? In that question,no answer uses += operator.And the difference between += and + operator

How to force std::stringstream operator >> to read an entire string?

半世苍凉 提交于 2019-11-30 04:03:36
问题 How to force std::stringstream operator >> to read an entire string instead of stopping at the first whitespace? I've got a template class that stores a value read from a text file: template <typename T> class ValueContainer { protected: T m_value; public: /* ... */ virtual void fromString(std::string & str) { std::stringstream ss; ss << str; ss >> m_value; } /* ... */ }; I've tried setting/unsetting stream flags but it didn't help. Clarification The class is a container template with

How do I use 3 and 4-byte Unicode characters with standard C++ strings?

南笙酒味 提交于 2019-11-30 02:16:00
In standard C++ we have char and wchar_t for storing characters. char can store values between 0x00 and 0xFF . And wchar_t can store values between 0x0000 and 0xFFFF . std::string uses char , so it can store 1-byte characters only. std::wstring uses wchar_t , so it can store characters up to 2-byte width. This is what I know about strings in C++. Please correct me if I said anything wrong up to this point. I read the article for UTF-8 in Wikipedia, and I learned that some Unicode characters consume up to 4-byte space. For example, the Chinese character 𤭢 has a Unicode code point 0x24B62 ,