由买买提看人间百态

topics

全部话题 - 话题: istream
1 (共1页)
X****r
发帖数: 3557
1
来自主题: Programming版 - some problems with "cin"
This is how istream works: you try to extract a number from it by
"cin >> n", but the next character is not a part of any number,
so istream refuses to extract it. The rouge character is kept in
the istream, and the error state of this istream object is set to
"fail". To resume extracting numbers from this istream, you have
to do both of the following:
1) clear the "fail" state of the istream by calling "cin.clear()"
2) remove the bad character(s) from the stream. The easiest
way is to call "cin
k**********g
发帖数: 989
2
来自主题: Programming版 - 问一段C++ iostringstream的代码

http://www.cplusplus.com/reference/string/string/operator%3E%3E
http://www.cplusplus.com/reference/istream/istream/
http://www.cplusplus.com/reference/ios/ios/good/
Usually, istream::good() is used to check the stream's state, because
istream actually has three other non-good states: eof(), bad(), and fail().
The boolean cast is usually not good enough.
Your test code shows that it is necessary to clear the string prior to
reading. So, Line 19 is necessary anyway.
A simpler alternative is to de... 阅读全帖
X***X
发帖数: 302
3
我做overloading 时遇到的问题,头都大了。
#include
using namespace std;
using std::ostream;
using std::istream;
class Array{
friend ostream &operator<<( ostream &, const Array &);
friend istream &operator>>( istream &, Array &);
public:
Array(int=10);
Array(const Array &);
Array &operator=(const Array &);
Array &operator+=(const Array &);
int &operator[](int);
private:
int length;
int *ptr;
};
然后一个cpp的文件,定义overloading:
#include
s*****k
发帖数: 604
4
来自主题: Programming版 - 困扰多时的MATLAB crash问题
困扰多时的MATLAB crash问题
谁有matlab的帮我运行一下下面的程序。看看是不是和我一样的情况。
平时用matlab比较多。最近在matlab帮助文挡里看了一点
如何在matlab里使用java类,在好奇心驱使下用matlab
写了一个简单的web服务器,原理就是调用java.net.serversocket。
其实我java基本不会,但是稍微看了看文档还是大概能明白
怎么使用ServerSocket类的。
我写了一个简单web服务器,运行正常,可以serve静态网页。
然后我又想改进一下以便这个服务器可以用matlab语言做脚本
产生动态网页。
然后我就修改代码,并且没有保存旧的代码,改了一会发现
一运行程序,matlab就会crash。连debug都没法做,你只要在
源文件里面设置断点就能导致matlab crash,然后matlab必须
关掉重新启动。我昨晚找了一晚上bug都不知道哪里有问题。
高手帮我看看下面的程序哪里会造成这个问题。
说明一下,我只要把循环改成 for k=1:2 和 end 注释掉就没问题了。
但是这样只能serve一个 浏览器的request了... 阅读全帖
g*****e
发帖数: 172
5
恩,终于搞懂了,原来这么简单.
在while (getline()) {}中,getline()返回的是一个istream,而在while()里面应该是一个bool,或者int,char,pointer.所以,compiler 就很辛勤地在istream的所有函数和继承的函数里面查找有没有把istream转变成bool,int,char 或者pointer返回的函数,于是找到了operator void*(),竟然是一个隐式转换.compiler真聪明勤劳.
用函数fail()检查failbit,badbit是否set.
operator void* () const {}
这个函数的返回值有2种,一种是null(也就是0),另外一种返回值是(void*)this.竟然把
this给cast成了一个generic pointer.

.
actually
A**u
发帖数: 2458
6
来自主题: Programming版 - 请教一个static 函数的问题
class A{}
class B: public A{}
class C:public D
{
public:
D(istream& str);
private:
static A* read(istream& str); //读入内容,返回A的指针
list component;
}
D::D(istream& str)
{
component.push_back(read(str));
}
请教这里 read函数 为什么是static的
有什么原因吗
y**b
发帖数: 10166
7
来自主题: Programming版 - 能否对某个库进行操作符重载?
再问一个问题,如果对__float128重载>>, 比如
std::istream& operator >> (std::istream& is, __float128& number) {
double val;
is >> val;
number = (__float128) val;
return is;
}
就没有必要再对ifstream重写这个函数了吧?因为ifstream继承于istream,
想确认一下。
多谢耐心回复。
s********l
发帖数: 998
8
来自主题: JobHunting版 - amazon第三轮电面
不知道你怎么定义multi inheritance
反正 是个多层的inheritance
比如 iostream 是继承了ostream & istream
ostream & istream是 virtual inheritance ios

inheritan
h*****g
发帖数: 312
9
来自主题: 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 可以输出到文件吗?网上没找到答案~~~
l***e
发帖数: 480
10
来自主题: Programming版 - 继续请教C++重载问题,>>
like this?:
friend istream & operator>>(istream &stream, class & obj) {
i=getline(stream, obj.a, '\t');
if(i>0){
getline(stream, obj.b, '\t');
stream >> obj.c >> obj.d >> ws;
return stream;
}
}
mail()
{
while(!cin.eof()) cin>>obj;
}
H***a
发帖数: 735
11
来自主题: Programming版 - c++之极弱问
The error happened at
while(cin>>v1)
Check this out: http://www.cplusplus.com/reference/iostream/istream/operator>>/
"Errors are signaled by modifying the internal state flags:
[flag]
failbit
[error]
The input obtained could not be interpreted as an element of the appropriate
type."
cin is an object of class istream, once error occurs, "an exception of type
ios_base::failure is thrown", cin therefore won't work anymore. That's why
v2 is still 0.
r****t
发帖数: 10904
12
来自主题: Programming版 - C++ Q13: Input
(2nd function version:)
istream& getline ( istream& is, string& str );
Get line from stream
...
The delimiter character is delim for the first function version, and '\n' (
newline character) for the second. The extraction also stops if the end of
file is reached in is or if some other error occurs during the input
operation.
所以 getline 就从 hello 后面那个空格开始读到 \n 了。
t****t
发帖数: 6806
13
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... 阅读全帖
g*****e
发帖数: 172
14
看到这种语法,我不懂:
while (getline(...)) { }
getline()返回值是一个istream&,用在while()语句里怎么来判断true or false?
istream是一个class,不是一个值啊.

.
)
text
t****t
发帖数: 6806
15
no, getline returns the istream you called with, and istream is cast to void
*, in turn convert to bool.
x******a
发帖数: 6336
16
来自主题: Programming版 - 请问关于overloading <<
I found if I make the second parameter const, i.e.std::ostream& operator<<(
std::ostream& os, const str& s), it worked now. what happened?
std::ostream& operator<<(std::ostream& os, str& s){
//obliterate existing value(s)
for (str::size_type i=0; i!=s.size(); ++i)
os<
return os;
}
class str{
public:
typedef std::vector::size_type size_type;
//constructors
str();
str(size_type n,char c);
str(const char* cp);
template str(In b, ... 阅读全帖
t*********u
发帖数: 26311
17
来自主题: ebiz版 - 10个包子求解答
写一个c
用istream 跳 pos
i**********e
发帖数: 1145
18
来自主题: JobHunting版 - FaceBook面经--第三(最后)部分
我以前贴过,但不知道为什么找不回那个帖子了。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in, indentLevel+TAB_SPACE);
c = in.get();
assert(c == '... 阅读全帖
i**********e
发帖数: 1145
19
来自主题: JobHunting版 - 问道 facebook 面试题
不是大侠,贴一贴我的代码,代码如果不好看 请多多包涵
恐怕只能 handle 以上的 sample test case。
基本思路就是递归,如果有更复杂的情况可以再慢慢改进。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in... 阅读全帖
i**********e
发帖数: 1145
20
来自主题: JobHunting版 - 问道 facebook 面试题
其实不递归 代码或许会更简单些
void outputJSon(istream &in) {
bool newLine = false;
bool inBracket = false;
int indentLevel = 0;
while (in) {
char c = in.get();
if (newLine) {
cout << endl << setw(indentLevel) << c;
newLine = false;
}
else {
cout << c;
}
if (c == '{') {
indentLevel += TAB_SPACE;
newLine = true;
} else if (!inBracket && c == ',') {
newLine = true;
} else if (c == '}') {
newLine = true;
} else if (c == '[') {
... 阅读全帖
c**********e
发帖数: 2007
21
来自主题: JobHunting版 - google的一道题求解
这是改过的。过了所有测试。复杂度O(n)。
#include
using namespace std;
inline int min(int a, int b) { return a < b ? a : b; }
int maxArea(int a[], int size) {
int mArea=0, area, left=0, right=size-1;
while(left area=(right-left) * min(a[left],a[right]);
if(area>mArea) mArea=area;
if(a[left]<=a[right]) {
int i=left;
while(i left=i;
} else {
int j=right;
while(j>left && a[j]<=a[right]) j--;
right=j;
}
}
... 阅读全帖
c**********e
发帖数: 2007
22
来自主题: JobHunting版 - google的一道题求解
这是改过的。过了所有测试。复杂度O(n)。
#include
using namespace std;
inline int min(int a, int b) { return a < b ? a : b; }
int maxArea(int a[], int size) {
int mArea=0, area, left=0, right=size-1;
while(left area=(right-left) * min(a[left],a[right]);
if(area>mArea) mArea=area;
if(a[left]<=a[right]) {
int i=left;
while(i left=i;
} else {
int j=right;
while(j>left && a[j]<=a[right]) j--;
right=j;
}
}
... 阅读全帖
i*********7
发帖数: 348
23
来自主题: JobHunting版 - A家面试题
我一直在想,所谓stream,是打算怎么表示?
难道是类似cin或者istream,然后读100万次?
h*******8
发帖数: 29
24
来自主题: JobHunting版 - 黑客rank Stock Maximize
搞了一个下午,一直超时,难道复杂度可以比O(n^2)小么。请教各位了。感觉自己实在
是太菜了。
代码是依据以下的方程,a[N]是input
f[i] = max( max(for all j, 0 (i-1)*a[i] - (a[1]+a[2]+...+a[i-1]) )
以下是代码
int main() {

int T;
#define RF
#ifdef RF
ifstream& in = ifstream("input.txt" , ios::in);
#else
istream& in = cin;
#endif
in>>T;
for(int t=0 ; t int N;
in>>N;
vector a(1,0);

vector cache(1 , 0);
int accu = 0;
... 阅读全帖

发帖数: 1
25
来自主题: 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
26
来自主题: Programming版 - What is wrong with the code?
我发现你老了...istream::get(char& c)...
X****r
发帖数: 3557
27
来自主题: Programming版 - how to let cin get enter
"get()" extracts *unformatted* data from the stream as character
or characters.
You seem to have lots of questions on istream. Why don't you just
read through the document for once? It only takes 15 minutes and
it would save lot of your time on these questions.

what?
X****r
发帖数: 3557
28
An istream (more precisely, an ios) can be automatic converted
to a void *. The result is a non-null pointer of the state of the stream
is good, or null otherwise. It is then implicitly converted to a bool as
the condition of the while statement, i.e. true if stream good.
c**********e
发帖数: 2007
29
来自主题: 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
H***a
发帖数: 735
30
来自主题: Programming版 - how to use cin as default ifstream?
cin is istream, just use it.
H***a
发帖数: 735
31
来自主题: Programming版 - how to skip the last empty lines in ifstream?
Np. My understanding is that (for your original code):
eof() or good() checks internal state flags "eofbit", which is modified when
getline() is encountered problem in reading from infile.
When getline() reads the last line, it stops by seeing the EOF, however it
consider this read is successful so won't flip eofbit to TRUE. The file
pointer moves to position of EOF (remember getline() returns istream&)
Since eofbit is still FALSE now, eof() or good() lets it loop over, getline(
) sees the EOF a... 阅读全帖
h********n
发帖数: 1671
32
istream& getline (char* s, streamsize n );
g*****e
发帖数: 172
33
对,我知道basic_istream,basic_ostream都是模板,istream,ostream是用的这2个模板.

.
)
text
t****t
发帖数: 6806
34
if istream& is cast to bool, then it basically test whether it's in good
state. if it is not in good state(), it could be eof() or read/write error.
most likely eof(). that's why i said you need to check opeator!() (actually
it's not operator!(), but operator void*(), then test whether it's NULL)
see http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.4
a***y
发帖数: 2803
35
我说的是getline()这个函数的返回值是istream&,compiler把它转变成了boolean.
b****n
发帖数: 2
36
来自主题: Programming版 - C++要是有null object就好了
#include
#include
#include "boost/optional.hpp"
using namespace std;
template
class Vector : public std::vector {
public:
using TT = boost::optional;
TT operator[](size_t index) const noexcept {
if(index >= this->size()) {
return boost::none;
} else {
return std::vector::operator [](index);
}
}
};
int main() {
Vector AAA;
if(!AAA[0]) cerr << "none" << endl;
AAA.push_back(1);
... 阅读全帖
z**********6
发帖数: 68
37
来自主题: Computation版 - 问个C++读入文件的问题
先确定文件是否成功打开,istream类里有对应的函数做判断
1 (共1页)