stdstring

How do you convert a C++ string to an int? [duplicate]

*爱你&永不变心* 提交于 2019-11-26 17:24:49
Possible Duplicate: How to parse a string to an int in C++? How do you convert a C++ string to an int? Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example). Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way. Randy Sugianto 'Yuku' #include <sstream> // st is input string int result; stringstream(st) >> result; Martin York Use the C++ streams. std::string plop("123"); std::stringstream str(plop); int x; str >> x; /* Lets not forget to error checking */ if (!str) { // The conversion failed. //

Using the less than comparison operator for strings

南笙酒味 提交于 2019-11-26 16:55:02
问题 I'm following a tutorial for C++ and looking at strings and overloading with operators such as +=, ==, != etc, currently have a simple if statement if(s1 < s2) cout << s2 <<endl; else if(s2 < s1) cout << s1 << endl; else cout << "Equal\n"; but how does this work, and how does the program decide which string is greater than another? looking around I've found a basic template decleration: template<class charT, class traits, class Allocator> bool operator< ( const basic_string<charT,traits

Concatenating strings doesn&#39;t work as expected [closed]

我怕爱的太早我们不能终老 提交于 2019-11-26 15:27:04
问题 I know it is a common issue, but looking for references and other material I don't find a clear answer to this question. Consider the following code: #include <string> // ... // in a method std::string a = "Hello "; std::string b = "World"; std::string c = a + b; The compiler tells me it cannot find an overloaded operator for char[dim] . Does it mean that in the string there is not a + operator? But in several examples there is a situation like this one. If this is not the correct way to

c++ integer->std::string conversion. Simple function?

余生颓废 提交于 2019-11-26 15:13:11
问题 Problem: I have an integer; this integer needs to be converted to a stl::string type. In the past, I've used stringstream to do a conversion, and that's just kind of cumbersome. I know the C way is to do a sprintf , but I'd much rather do a C++ method that is typesafe(er). Is there a better way to do this? Here is the stringstream approach I have used in the past: std::string intToString(int i) { std::stringstream ss; std::string s; ss << i; s = ss.str(); return s; } Of course, this could be

What are some algorithms for comparing how similar two strings are?

故事扮演 提交于 2019-11-26 15:09:25
问题 I need to compare strings to decide whether they represent the same thing. This relates to case titles entered by humans where abbreviations and other small details may differ. For example, consider the following two titles: std::string first = "Henry C. Harper v. The Law Offices of Huey & Luey, LLP"; As opposed to: std::string second = "Harper v. The Law Offices of Huey & Luey, LLP"; A human can quickly gauge that these are most likely one and the same. The current approach I have taken is

Error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’

耗尽温柔 提交于 2019-11-26 11:56:40
问题 At the top of my file I have #define AGE \"42\" Later in the file I use ID multiple times including some lines that look like 1 std::string name = \"Obama\"; 2 std::string str = \"Hello \" + name + \" you are \" + AGE + \" years old!\"; 3 str += \"Do you feel \" + AGE + \" years old?\"; I get the error: \"error: invalid operands of types ‘const char [35]’ and ‘const char [2]’ to binary ‘operator+’\" on line 3. I did some research and found it was because of how C++ was treating the different

How do you construct a std::string with an embedded null?

自作多情 提交于 2019-11-26 11:42:46
If I want to construct a std::string with a line like: std::string my_string("a\0b"); Where i want to have three characters in the resulting string (a, null, b), I only get one. What is the proper syntax? Since C++14 we have been able to create literal std::string #include <iostream> #include <string> int main() { using namespace std::string_literals; std::string s = "pl-\0-op"s; // <- Notice the "s" at the end // This is a std::string literal not // a C-String literal. std::cout << s << "\n"; } Before C++14 The problem is the std::string constructor that takes a const char* assumes the input

Is it possible to use std::string in a constexpr?

拈花ヽ惹草 提交于 2019-11-26 10:18:34
问题 Using C++11, Ubuntu 14.04, GCC default toolchain . This code fails: constexpr std::string constString = \"constString\"; error: the type ‘const string {aka const std::basic_string}’ of constexpr variable ‘constString’ is not literal... because... ‘std::basic_string’ has a non-trivial destructor Is it possible to use std::string in a constexpr ? (apparently not...) If so, how? Is there an alternative way to use a character string in a constexpr ? 回答1: No, and your compiler already gave you a

Is std::string size() a O(1) operation?

天大地大妈咪最大 提交于 2019-11-26 09:54:41
问题 Is std::string size() a O(1) operation? The implementation of STL I\'m using is the one built into VC++ 回答1: If you're asking if MSVC's implementation of string::size() has constant complexity, then the answer is yes. But Don Wakefield mentioned Table 65 in 23.1 of the C++ Standard where it says that the complexity of size() should follow what's said in 'Note A'. Note A says: Those entries marked ‘‘(Note A)’’ should have constant complexity. However, that does not mean that those entries

std::string::c_str() and temporaries

岁酱吖の 提交于 2019-11-26 09:32:12
问题 Is the following C++ code well-formed: void consumer(char const* p) { std::printf(\"%s\", p); } std::string random_string_generator() { // returns a random std::string object } consumer(random_string_generator().c_str()); The problem I have with it is, that after creating the temporary std::string object and taking the c_str() pointer, nothing prevents the std::string object from getting destroyed (or maybe I\'m wrong?). Can you please point me to the standard, if the code is OK despite