cout

how does cout << actually work?

这一生的挚爱 提交于 2019-12-17 17:45:12
问题 I was wondering how std::cout is able to use << as it does. My main puzzlement is with whether std::cout as an instance of something. Basically, how is << defined? If I do this for a custom class, I need an instance of some sort... I could see implementing it as kind of a hack with void pointers or something, but I'd like to see the actual way it's done. Does anyone here know? Thanks 回答1: std::cout is an instance of std::ostream . std::cout << "something" calls one of the operator<< overloads

When does cout flush?

萝らか妹 提交于 2019-12-17 14:45:48
问题 I know endl or calling flush() will flush it. I also know that when you call cin after cout , it flushes too. And also when the program exit. Are there other situations that cout flushes? I just wrote a simple loop, and I didn't flush it, but I can see it being printed to the screen. Why? Thanks! for (int i =0; i<399999; i++) { cout<<i<<"\n"; } Also the time for it to finish is same as with endl both about 7 seconds. for (int i =0; i<399999; i++) { cout<<i<<endl; } 回答1: There is no strict

Using << operator to write to both a file and cout

六眼飞鱼酱① 提交于 2019-12-17 09:58:40
问题 I'd like to overload << operator to write the value it takes to a file and cout. I have tried to do it with following code, but couldn't succeed it. It just writes the value to text file. Any help would be appreciated. Thank you. void operator<<(std::ostream& os, const string& str) { std::cout << str; os << str; } int main() { ofstream fl; fl.open("test.txt"); fl << "!!!Hello World!!!" << endl; return 0; } 回答1: Create a helper class and overload operators that takes care of streaming to two

Order of execution in operator <<

六月ゝ 毕业季﹏ 提交于 2019-12-17 06:54:03
问题 I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below A1B2 While I can see that the output I get is BA12 I thought that the call std::cout<< b->fooA() << b->fooB() << std::endl was equivalent to call std::cout.operator<<( b->fooA() ).operator<< ( b->fooB() ) but I can see that this is not the case. Can you help me understanding better how this does it work and the relationship with the global operator<< ? Is this last ever called

Hide user input on password prompt [duplicate]

老子叫甜甜 提交于 2019-12-17 06:41:28
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Read a password from std::cin I don't work normally with the console, so my question is maybe very easy to answer or impossible to do . Is it possible to "decouple" cin and cout , so that what I type into the console doesn't appear directly in it again? I need this for letting the user typing a password and neither me nor the user normally wants his password appearing in plaintext on the screen. I tried using

std::cout won't print

夙愿已清 提交于 2019-12-17 03:43:18
问题 Is there any circumstance when std::cout << "hello" doesn't work? I have a c/c++ code, however the std::cout doesn't print anything, not even constant strings (such as "hello"). Is there any way to check if cout is able/unable to open the stream? There are some member functions like good() , bad() , ... but I don't know which one is suitable for me. 回答1: Make sure you flush the stream. This is required because the output streams are buffered and you have no guarantee over when the buffer will

STL标准库-容器-unordered_set

我与影子孤独终老i 提交于 2019-12-16 17:57:47
技术在于交流、沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 unordered_set与与unordered_map相似,这次主要介绍unordered_set unordered_set它的实现基于hashtable,它的结构图仍然可以用下图表示,这时的空白格不在是单个value,而是set中的key与value的数据包 有unordered_set就一定有unordered_multiset.跟set和multiset一样,一个key可以重复一个不可以 unordered_set是一种无序集合,既然跟底层实现基于hashtable那么它一定拥有快速的查找和删除,添加的优点.基于hashtable当然就失去了基于rb_tree的自动排序功能 unordered_set无序,所以在迭代器的使用上,set的效率会高于unordered_set template<class _Value, class _Hash = hash<_Value>, class _Pred = std::equal_to<_Value>, class _Alloc = std::allocator<_Value> > class unordered_set : public __unordered_set<_Value, _Hash, _Pred, _Alloc> { typedef _

C++ vector使用实例

人盡茶涼 提交于 2019-12-15 20:21:32
C++ vector #include <iostream> #include <vector> #include <string> #include <algorithm> #include <iterator> using namespace std; int main() { vector<string> vec_str; vec_str.reserve(6); vec_str.push_back("C++,"); vec_str.insert(vec_str.end(), {"test1","test2","test3","test4"}); copy(vec_str.cbegin(), vec_str.cend(), ostream_iterator<string>(cout, " ")); cout << endl; cout << "max_size:" << vec_str.max_size() << endl; cout << "size:" << vec_str.size() << endl; cout << "capacity:" << vec_str.capacity() << endl; swap(vec_str[1], vec_str[3]); vec_str.insert(find(vec_str.begin(),vec_str.end(),

C++ 标准输入输出函数

北城以北 提交于 2019-12-15 14:26:22
标准输入函数:cin cin >> elementType ; C++采用流式输入法,将所有的输入看作流入; cin 函数无需标识符来指定特定的数据类型,同时为了兼容C语言的 scanf 函数,导致 cin 函数的输入效率较低,为了提高效率可以使用以下语句解除C++标准输入函数与C标准输入函数的绑定。但是一旦使用此语句,程序中不可以同时使用 cin 与 scan f函数,否则会发生未知错误。 ios :: sync_with_stdio ( false ) ; cin 函数使用较为简单,不做过多阐述。 标准输出函数:cout cout << element << endl ; cout << "data" << endl ; cout << "data" << element << endl ; 同时 cout 为了兼容 printf 效率也较低,解决方法同 cin ; cout标准化输出 # include <iomanip> 控制符 功能 left 设置左对齐 right 设置右对齐 setprecision( n ) 设置浮点数的有效数字位数 fixed 1.将精度设置转向小数点位数,2. 强制显示小数点 6 位(缺省精度) showpoint 强制使用0补全(有效数字位为 6 位) setw( n ) 设置位宽 # include <iostream> # include

异常类的构建——

£可爱£侵袭症+ 提交于 2019-12-14 00:14:37
1.Exception.h 中增加ArithmetricException类 class ArithmetricException:public Exception { public: ArithmetricException():Exception(0) {} //我认为这个实现没有必要 ArithmetricException(const char* message):Exception(message) {} ArithmetricException(const char* file, int line):Exception(file, line) {} ArithmetricException(const char* message, const char* file, int line):Exception(message,file,line) {} ArithmetricException(const ArithmetricException& e):Exception(e) {} ArithmetricException& operator = (const ArithmetricException& e) { Exception::operator =(e); return *this; } }; main.cpp #include <iostream>