std

operator << overload

自古美人都是妖i 提交于 2019-12-06 06:55:21
//using namespace std; using std::ifstream; using std::ofstream; using std::cout; class Dog { friend ostream& operator<< (ostream&, const Dog&); public: char* name; char* breed; char* gender; Dog(); ~Dog(); }; im trying to overload the << operator. I'm also trying to practice good coding. But my code wont compile unless i uncomment the using namespace std. i keep getting this error and i dont know. im using g++ compiler. Dog.h:20: error: ISO C++ forbids declaration of ‘ostream’ with no type Dog.h:20: error: ‘ostream’ is neither function nor member function; cannot be declared friend. if i add

Why is “using namespace std;” considered bad practice?

和自甴很熟 提交于 2019-12-06 06:08:58
I've been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Does it impact performance? Greg Hewgill This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar: using namespace foo; using namespace bar; Everything works fine, and you can call Blah() from Foo and Quux() from Bar

What does rd stand for in rdstate and rdbuf?

蹲街弑〆低调 提交于 2019-12-06 06:04:28
问题 There are two names in C++ standard I/O library: rdstate and rdbuf . I know "state" and "buf", but what is "rd"? (PS: I believe I know how to use rdstate and rdbuf , don't teach me that). 回答1: They stand for "read" I think, similar to how most people use "getXXX". 来源: https://stackoverflow.com/questions/14080153/what-does-rd-stand-for-in-rdstate-and-rdbuf

std::unique and removing duplicates from a container of objects

你。 提交于 2019-12-06 05:33:00
问题 I would like to know if there is an efficient way to remove objects from a container based on values of member fields of the objects. For example, I can do the following using stl::unique with a list of strings: #include<iostream> #include<list> #include<string> #include<algorithm> using namespace std; bool stringCompare(const string & l, const string & r) { return (l==r); } int main() { list<string> myStrings; myStrings.push_back("1001"); myStrings.push_back("1001"); myStrings.push_back("81"

Why does std::setprecision(6) stream more than six digits in fixed-width mode?

删除回忆录丶 提交于 2019-12-06 04:56:43
问题 The output of the following code: #include <limits> #include <iostream> #include <iomanip> #include <limits> #include <string> #include <sstream> using namespace std; inline string lexical_cast(const float arg) { stringstream ss; ss << fixed << setprecision(numeric_limits<float>::digits10) << arg; if (!ss) throw "Conversion failed"; return ss.str(); } int main() { cout << numeric_limits<float>::digits10 << '\n'; cout << lexical_cast(32.123456789) << '\n'; } is: 6 32.123455 I expected, and

A question regarding the implementation of std::add_pointer

浪子不回头ぞ 提交于 2019-12-06 04:09:59
From std::add_pointer Possible implementation namespace detail { template <class T> struct type_identity { using type = T; }; // or use std::type_identity (since C++20) template <class T> auto try_add_pointer(int) -> type_identity<typename std::remove_reference<T>::type*>; template <class T> auto try_add_pointer(...) -> type_identity<T>; } // namespace detail template <class T> struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {}; The description for the above (possible) implementation reads: If T is a reference type, then provides the member typedef type which is a pointer to the

what is the best way to use the C type uuid_t as a key in a std::map?

对着背影说爱祢 提交于 2019-12-06 02:46:32
问题 Is this an appropriate way to provide unique keys in a map? In other words, is the key being made from the unique value contained in the uuid, or is it being made from the pointer to the uuid_t struct? A side question, is there a more efficient container, when I don't care about the ordering by keys inside the container? #include <uuid/uuid.h> int main(int argc, char **argv) { std::map<uuid_t,int> myMap; uuid_t id1; uuid_t id2; uuid_generate( (unsigned char *)&id1 ); uuid_generate( (unsigned

Why doesn't std::string take a null pointer?

只谈情不闲聊 提交于 2019-12-06 01:54:12
I recently passed a null pointer to a std::string constructor and got undefined behavior. I'm certain this is something that thousands or tens of thousands of programmers have done before me, and this same bug has no doubt crashed untold numbers of programs. It comes up a lot when converting from code using char* to code using std::string , and it's the kind of thing that is not catchable at compile time and can easily be missed in run time unit tests. What I'm confused about is the reason for specifying std::string this way. Why not just define std::string(NULL)=="" ? The efficiency loss

error: there are no arguments to 'at' that depend on a template parameter, so a declaration of at must be available

余生颓废 提交于 2019-12-06 01:52:21
问题 Noob here, I'm trying to compile this segment of code from Bjarne Stroustrup's 'The C++ Programming Language' but CodeBlocks keeps throwing me this error. The code is about range checking an array held in a vector function. Here is the code: #include <iostream> #include <vector> #include <array> using namespace std; int i = 1000; template<class T> class Vec : public vector<T> { public: Vec() : vector<T>() { } T& operator[] (int i) {return at(i); } const T& operator[] (int i) const {return at

C++: Why does space always terminate a string when read?

为君一笑 提交于 2019-12-06 01:44:45
问题 Using type std::string to accept a sentence, for practice (I haven't worked with strings in C++ much) I'm checking if a character is a vowel or not. I got this: for(i = 0; i <= analyse.length(); i++) { if(analyse[i] == 'a' || analyse[i] == 'e' [..etc..]) { ...vowels++; } else { ... ...consonants++; } This works fine if the string is all one word, but the second I add a space (IE: aeio aatest ) it will only count the first block and count the space as a consonant, and quit reading the sentence