由买买提看人间百态

topics

全部话题 - 话题: ostream
首页 上页 1 2 (共2页)
t****t
发帖数: 6806
1
no, most ppl don't need that. just know istream and ostream and their
variations is enough (istream is basic_istream, similar for ostream).
their variations include ifstream/ofstream, istringstream/ostringstream.
just know op<< for output, op>> for input, op! (usually you need that
instead of eof() member) to check eof, clear() for opening another file,
manipulators (such as setw(), endl, hex, etc.) for formating, and getline()
for reading a whole line. these are enough for 90% of ppl deal... 阅读全帖
l*********s
发帖数: 5409
2
来自主题: Programming版 - please help debug this code
MSVC老是抱怨没有《右边是TEntry类型的重载,但是明明一开始就重载了啊,到底怎么
回事请大家看看。
(这段代码来自于Think in C++)
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef map> Thesaurus;
typedef pair> TEntry;
typedef Thesaurus::iterator TIter;
ostream& operator<<(ostream& os, const TEntry& t){
os << t.first <<": ";
ostream_iterator out_it(os," ");
copy(t.second.begin(),... 阅读全帖
y**b
发帖数: 10166
3
来自主题: Programming版 - 能否对某个库进行操作符重载?
GCC quad precision math lib定义了一个数据类型__float128,
头文件为, 库名为libquadmath.so.
可是这个数据类型没有重载<<,输出时候有两个办法,一是调用某个函数转成
字符串进行输出,再一个就是进行类型转换(double)data再输出,可是对我
原有程序改动太大。有没有什么简单的办法比如重载<<?
ostream& operator << (ostream& os, const __float128 &r) {
os << (double)__float128;
return os;
}
k*******3
发帖数: 1909
4
来自主题: Programming版 - 请教C++程序中的Maze().exitMaze();
下面这个程序来自Data structures and algorithms in C++ by Adam Drozdek
第四章的迷宫(maze)程序。
我的疑问是最后几行main函数里面的Maze().exitMaze();怎么解释,我想不通语法是怎
么样的。
Maze()是调用哪个函数呢?如果是Maze class的Maze() constructor,难道不用先定义
一个Maze 对象吗?
谢谢。
#include
#include
#include
using namespace std;
template
class Stack : public stack {
public:
T pop() {
T tmp = top();
stack::pop();
return tmp;
}
};
class Cell {
public:
Cell(int i = 0, int j = 0) {
x = i; y... 阅读全帖
k****r
发帖数: 9629
5
http://www.flickr.com/groups/dcphotorights/discuss/721576019780
The Pentagon
philliefan99 says:
When I went out to take pics of the Pentagon on 9/11/07, I first went to a g
as station that had an unobstructed view of the building. As I was unfolding
my tripod, a Pentagon Police officer stopped me and told me that this was p
rivate property, and that it was illegal to take pictures of the Pentagon. I
left without making a stink about it, and immediately drove up the street t
o the Air Force Me... 阅读全帖
B*****t
发帖数: 335
6
my code for first problem, not tested.
const int N = 1000;
int b[N], a[N];
int n;
void dfs(int a[], int cur, int level) {
if(level==k) {
copy(b, b+k, ostream(cout," "));
cout< return;
}
int i, j;
for(i=cur+1; i<=n; i++) {
b[level+1] = a[i];
dfs(a, i, level+1);
}
}
P*******b
发帖数: 1001
7
来自主题: JobHunting版 - C++ Q22: ostream
c
P*******b
发帖数: 1001
8
来自主题: JobHunting版 - C++ Q22: ostream
c)
M********5
发帖数: 715
9
来自主题: JobHunting版 - C++ Q22: ostream
我也觉得是c
l*******o
发帖数: 791
10
来自主题: JobHunting版 - C++ Q22: ostream
C
c**********e
发帖数: 2007
11
来自主题: JobHunting版 - C++ Q22: ostream
You guys are all right.
c) There is no operator<< defined for Base.
This is the correct answer. The code will fail to compile because you try
to output a Base object, and there is no corresponding operator<<.
h*****g
发帖数: 312
12
来自主题: JobHunting版 - C++ 一问?
You have to implement a function that needs to be able to write to both
standard output (typically the console screen) and files. Which one of the
following function declarations satisfies that need?
A.
void print(std::ostream &os);
B.
void print(std::ofstream is);
C.
void print(std::cout);
D.
void print(std::istream is);
E.
void print(std::istream &is);
问下,B 为啥不行呢?A 可以输出到文件吗?网上没找到答案~~~
r******n
发帖数: 170
13
来自主题: JobHunting版 - reverse链表
假如我写了个linklist的类,如下:
template
struct Node
{
T data;
Node* next;
};
template
class List
{
public:
List():head(NULL){};
List(const Node *inNode):head(inNode){};
~List(){destroy();};
void addToHead( const T &item );
void print( ostream &out ) const;
void reverse();
void destroy();
private:
Node *head;
};
//iterative reverse
template
void List::reverse()
{
if (!head) //empty list
return;
Node *prev=N... 阅读全帖
c**********e
发帖数: 2007
14
来自主题: JobHunting版 - C++ Q 99-102
C++ Q 99: global and static
What type of linkage does global variables and functions preceded by
the storage class specifier static have?
a. Internal
b. Intern
c. External
d. Extern
Answer: a
C++ Q 100: directives
Any number of which of the following directives can appear between
the #if and #endif directives?
a. #elif
b. #endif
c. #else
d. #if
Answer: a
C++ Q101: file stream
Multiple choice:
Which of the following file streams do not require a mode parameter
to be specified when opening a file ... 阅读全帖
e*****r
发帖数: 93
15
来自主题: JobHunting版 - 被鄙视了, c++基础知识
其实还能更简单一点
ostream& out=(cout<<"AA"<
e*****r
发帖数: 93
16
来自主题: JobHunting版 - 被鄙视了, c++基础知识
其实还能更简单一点
ostream& out=(cout<<"AA"<
x******a
发帖数: 6336
17
来自主题: JobHunting版 - 请问关于overloading << (转载)
【 以下文字转载自 Programming 讨论区 】
发信人: xiaojiya (xiaojiya), 信区: Programming
标 题: 请问关于overloading <<
发信站: BBS 未名空间站 (Wed Feb 27 11:59:50 2013, 美东)
I am working on the example in accelerated C++.
I have overloaded operator+= , operator+ , and operator<<.
the following code does not work.
I got "invalid operands to binary expression('ostream'(aka 'basic_ostream<
char>') and 'myString') when compiling the code.
myString s1="hello";
myString s2="world";
std::cout<< s1+s2 < it worked in the foll... 阅读全帖
A**u
发帖数: 2458
18
来自主题: JobHunting版 - 发发面经 攒人品 C++的
这公司,面了3轮电话面试,全是C++

1. constructor可以private吗? 怎么创建对象?
2. destructor可以private吗? 怎么销毁对象?
3. 定义 "<<" 输出; 为什么返回是ostream& << ? 为什么不能返回 void?
为什么定义成非成员函数?
4. 创建数组, 调用哪个构造函数?
唉, C++工作真没意思,老纠结这种东西
r*********n
发帖数: 4553
19
来自主题: JobHunting版 - 发发面经 攒人品 C++的
why is extractor defined as a non-member operator?
because the first operand is an ostream obj?
r*********n
发帖数: 4553
20
来自主题: JobHunting版 - 发发面经 攒人品 C++的
we are talking about the same thing
this gets back to my original post, the first operand is an ostream obj
b******7
发帖数: 92
21
来自主题: JobHunting版 - 一个新鲜twitter面经
几个类的数据直接暴露在外面,可见性、一致性的控制得不到保证
log类设计得不好,打印函数不应集中在log类中,也不好做访问控制
用c++的话,可仿照ostream重载<<的方法,对每个类重载一个写log方法
friend Logger & operator<<(Logger & log, const Account & acc);
friend Logger & operator<<(Logger & log, const Campaign & ca);
...
用java的话,增加logable接口
interface logable{
public void printLog(Logger log);
}
Account、Campaign等实现这个接口

发帖数: 1
22
来自主题: JobHunting版 - Google Japan电面
国内大三下学生,投了Google Japan 求RP, 感觉是跪了。
此外,求大神们内推,邮箱christopherwuy at gmail.com
简介: C > C++ = PHP > Python = R
Contributor of WineHQ, had sent more than 50 patches about VC++ runtime(
msvcr/msvcp). Some of them are the implementation of tr2::Filesystem
Library, tests of tr2::Threads and implementation of complex istream and
ostream::operator.
See http://goo.gl/Rn8eaW
Accepted by Google Summer Of Code 2015, project is implementing Filesystem
functions from tr2 namespace on Wine.
我的简历在LinkedIn:h... 阅读全帖
t****t
发帖数: 6806
23
来自主题: Programming版 - C++中怎么传递std::hex这样的参数啊
these are io manipulators. they are actually function pointers. for example,
ostream::operator<< actually has following overloads:
/* template <...> class basic_ostream { */
basic_ostream& operator<<
(basic_ostream& (*pf)(basic_ostream&));
basic_ostream& operator<<
(basic_ios& (*pf)(basic_ios&));
basic_ostream& operator<<
(ios_base& (*pf)(ios_base&));
t****t
发帖数: 6806
24
来自主题: Programming版 - c++ template中如何判断类型
真的要用printf的话,你可以先用ostringstream得到string,再用printf输出
不混合printf和ostream倒是对的.
虽然我觉得这样的代码非常傻

than
P********e
发帖数: 2610
25
来自主题: Programming版 - C++ template question with friend ostream
put that outside definition of operator << in .h file
j*****k
发帖数: 1198
26
来自主题: Programming版 - iterator一问
template
void lstack::print(ostream& os)
{
if(data==NULL) return;
vector::iterator i; // error: expected `;' before ‘i’
for(i=data->begin(); iend();i++) //error: ‘i’ was not
declared in this scope
os< os< return;
};
大家帮忙看看,这两个错误怎么回事?不解。用的是gcc version 4.1.3 20070929
t****t
发帖数: 6806
27
来自主题: Programming版 - ofstream and cout question
you can use
ostream& myout=cout;
but you can't change it. if you want to change it, use another scope.
g*****u
发帖数: 298
28
来自主题: Programming版 - C++ template function一个问题
想打印一个STL container里的元素,但不知道是哪种container,应该怎么写?写成下
面的G++编译不通过,VC8可以。
template
void printContainer(ostream& os, const T& con)
{
T::const_iterator iter = con.begin();
while(iter != con.end())
{
os<<*iter<<" ";
iter++;
}
os< }
a*n
发帖数: 32
29
来自主题: Programming版 - C++ template function一个问题
How about use copy?
template
void printContainer(ostream& os,const T& con)
{
ostream_iterator out(os," ");
copy(con.begin(), con.end(),out);
}
r*********r
发帖数: 3195
30
来自主题: Programming版 - 在帮忙看看这个吧 C: int->char*
看看 operator<<(ostream&, char ) 的定义.
z****e
发帖数: 2024
31
来自主题: Programming版 - C++ template question
如果你是编译器,看到forward 已经就是一个模板了,紧接着后面又看到一个,作
何感想?
template std::ostream& operator<<, 明明白白说了是个模板,后面再
加一个是画蛇添足吗?但是善良的编译器,会最大耐心的去理解你,会认为你是最
有聪明才智的人,不会犯这种错误,肯定是你有特殊的目的,才会告诉编译器,这不仅
仅是个模板,而且要用类型T,这个特殊的目的就是specialization。
但是,编译器猛然发现,你根本没有定义普通形式的模板,而且这个类型也不存在
,啊!原来你刷我,我就和死磕。
c**********e
发帖数: 2007
32
来自主题: Programming版 - C++ Q13: Input
#include
#include
#include
#include
using namespace std;
int main()
{
std::string line1(3, '*'), line2(5, '*');
std::cin >> line1;
std::getline(std::cin, line2);
std::cout << "Line 1:" << line1 << std::endl;
std::cout << "Line 2:" << line2 << std::endl;
std::cout << "EOF\n";
return 1;
}
Given the program above, what happens if, when the program is run, the user
enters "Hello world!" followed by a newline?
a) It will print out:
Line 1: Hello
Line 2: world!
EOF
e****d
发帖数: 895
33
来自主题: Programming版 - stl Compare为何需要重载()?
Usually, you should overload operator << for cout or any ostream.
In this case, if operator int conflicts with operator float, there
will be ambigious error.
g*****e
发帖数: 172
34
对,我知道basic_istream,basic_ostream都是模板,istream,ostream是用的这2个模板.

.
)
text
r*******y
发帖数: 1081
35
来自主题: Programming版 - about namespace
//1.cpp
#include
// using namespace std;
int main(){cout<<1;}
this file can not be compiled since cout is not declared in this scope.
I read the header file iostream and find a declare for cout:
extern ostream cout;
It seems cout is declared in the header file iostream, but
why we also need to write down
using namespace std; ?
thanks.
c**********e
发帖数: 2007
36
来自主题: Programming版 - C++ Q 99-102 (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q 99-102
发信站: BBS 未名空间站 (Fri Oct 28 23:14:06 2011, 美东)
C++ Q 99: global and static
What type of linkage does global variables and functions preceded by
the storage class specifier static have?
a. Internal
b. Intern
c. External
d. Extern
Answer: a
C++ Q 100: directives
Any number of which of the following directives can appear between
the #if and #endif directives?
a. #elif
b. #endif
c. #else
d. #if
Answer: a
C++ Q101... 阅读全帖
t****t
发帖数: 6806
37
来自主题: Programming版 - 能否对某个库进行操作符重载?
在绝大多数情况下(99%), operator<< 不是被输出类的成员, 因为第一个参数是
ostream&. 你说的"在类的实现代码内部"是什么意思?

动,
k*******3
发帖数: 1909
38
来自主题: Programming版 - return Triangular_iterator( _beg_pos );意思
下面程序copy来自C++ essential 4.7节,
Triangular_iterator begin() const
{ return Triangular_iterator( _beg_pos ); }
请问
return Triangular_iterator( _beg_pos ); 这句话的语法是怎么样的?
Triangular_iterator是自定义的一个class吧,class后面直接加(_beg_pos)什么意思
呢?
如果是返回一个Triangular_iterator的object, 不需要定义成
Triangular_iterator obj(_beg_pos);
return obj;吗?
谢谢!
================================Full Class definition==================
class Triangular_iterator
{
public:
Triangular_iterator( int index ) : _index( index-1 ){} //*... 阅读全帖
x******a
发帖数: 6336
39
来自主题: Programming版 - 请问关于overloading <<
I am working on the example in accelerated C++.
I have overloaded operator+= , operator+ , and operator<<.
the following code does not work.
I got "invalid operands to binary expression('ostream'(aka 'basic_ostream<
char>') and 'myString') when compiling the code.
myString s1="hello";
myString s2="world";
std::cout<< s1+s2 < it worked in the following way.
myString s1="hello";
myString s2="world";
myString s=s1+s2;
std::cout<< s < what should I do to make the first one work... 阅读全帖
a*********a
发帖数: 3656
40
来自主题: Programming版 - C++糟粕和需要避免的。
函数名和类名编译的时候总是要mangle的。如果你对over loaded function name
decoration不满意,那等你看到编译后的class name,岂不是要吐血?如果你的函数不
需要从别的语言调用,那name mangling完全无关。如果要从别的语言调用,那name
mangling是很多问题中极其trivial的一个。
“只能重载基本类型。”是啥意思? 一个ostream::operator<< 就不知道被多少自定
义的高级类重载了。
关于“函数调用前后悄悄塞自动代码。” c++传值给了你3种选择,by value,by
reference,by pointer。后两种都不调用copy constructor。如果细分的话,by
reference to const和by pointer to const在语义上和非 const的有显著区别。有的
语言,只支持by value或by reference。前者无法改变传递过来的参数,后者如果你要
在函数内部对参数操作但是不改变call site的变量值,那还是得自己做deep copy。
N******n
发帖数: 3003
41
来自主题: Biology版 - 谁装过python的limix package (转载)
/Developer/SDKs/MacOSX10.5.sdk/usr/include/architecture/i386/math.h:477:17:
note: 'lroundl' declared here
extern long int lroundl(long double);
^
In file included from ./src/limix/covar/combinators.cpp:15:
In file included from ./src/limix/covar/combinators.h:19:
In file included from src/limix/covar/covariance.h:18:
In file included from src/limix/types.h:18:
In file included from External/Eigen/Dense:1:
In file included from External/Eigen/Core:28:
In file included from /Applic... 阅读全帖
f*****s
发帖数: 141
42
来自主题: Quant版 - A C++ quiz question
Question: Which of the following statements correctly describe the C++
language and its libraries?
A. It is possible to declare a function pointer to the constructor of a C++
class.
B. Though one can override the << operator of the ostream class, it is not
possible to do so for the ofstream class because it does not use a buffered
output stream.
C. The C++ language does not have built in features for persisting or
serializing objects to disk and back.
D. It is not possible to declare a pointe
首页 上页 1 2 (共2页)