由买买提看人间百态

topics

全部话题 - 话题: endles
首页 上页 1 2 3 4 5 6 7 8 9 10 (共10页)
z****e
发帖数: 2024
1
来自主题: Programming版 - C++的一个问题
run the following!
i didn't see any problem.
#include
using std::cout;
using std::endl;
class A{
static int cnt;
int id;
public:
A():id(++cnt){}
void DoSomething() {cout< };
int A::cnt=0;
void foo(A *a)
{
int i;
for(i=0;i<10;i++)
a[i].DoSomething();
}
int main()
{
A a[10];
foo(a);
}
z****e
发帖数: 2024
2
class A{
int a;
vector v;
public:
A() : a(0),v(vector(10)) {
cout< }
};
or:
A::A() : a(0),v(10) {
cout< }
d*******n
发帖数: 524
3
我运行了下面的code
void f(int a[]) {
cout << "inside f:" << sizeof a << endl;
}
int main() {

int a[] = {2,3,4,5,6,7,8,9};
cout << "outside: " << sizeof a << endl;
f(a);
char e;
cin >> e;
return 0;
}
发现输出是:
outside: 32
inside f:4
也就是说在f内部用sizeof的话得到不是这个数组的size
那么在数组内部如何得到数组的size呢?
s*******1
发帖数: 20
4
来自主题: Programming版 - C++ delete
What happens after delete? It seems that the object still exists after
delete in the program below.
Thanks.
#include
using namespace std;
class C{
public:
void f(){cout << "exist!" << endl;}
int x;
};
int main()
{
C* Cptr = new C;
Cptr->x=1;
delete Cptr;
Cptr->f();
cout << Cptr->x << endl;
return 0;
}
//output
exist!
1
z****e
发帖数: 2024
5
来自主题: Programming版 - 问个char * 的问题
cout< cout<
z****e
发帖数: 2024
6
来自主题: Programming版 - C++问题
class base0{
public:
virtual void f(){
cout<<"b0"< }
};
class derived0:public base0{
public:
derived0(){
base0::f();//有什么问题么?
f();
}
virtual void f(){
cout<<"d0"< }
};
Q:
is it a good idea to call base class virtual function in derived class ctor?
any potential problem? or it is perfect OK?
s******5
发帖数: 141
7
int main(){
cout< cout< }
char* test1(){
char* tt = "test";
return tt;
}
char* test2(){
char tt[] = "TEST";
return tt;
}
为什么test1会正确的返回tt呢?它指向的不应该是local变量么?
多谢。
D******4
发帖数: 47
8
来自主题: Programming版 - 请问一个入门级 dynamic memory 的问题
今天看到dynamic memory,测试如下程序:
#include
#include
#include
using namespace std;
ifstream fin("test.in");
ofstream fout("test.out");
int main (){
int i = 0;
int n = 0;
int* p;

if(fin.is_open()){
fin>>n;
fout<<"n is: "< p = new (nothrow) int[n];
if (p == 0)
fout << "Error: memory could not be allocated"< else{
for (i=0; i> p[i];
fin.close();
fout << "You have ent
N***m
发帖数: 4460
9
来自主题: Programming版 - 问题
自己随便写的。但是不明白为什么say((const B*)&d2)不显式调用
operator const B*() in D2。难道slicing的优先级高抑或其他?
我概念不太清楚,望指教!
==========================================================
#include
using namespace std;
class B {
public:
virtual void say() const =0;
};
class D1 :public B {
string s;
public:
virtual void say() const
{
cout<<"bark!"< }
};
class D2 :public B {
string s;
public:
virtual void say() const
{
cout<<"bark! bark!"< }
operator const B*()
{
const B *
N***m
发帖数: 4460
10
来自主题: Programming版 - 小问题
c++ forbids implicit conversion of user-defined type more than once.
所以试验了一下。
首先是不是只能这样写?能不能用member function之类的?
假定ABC没有继承关系。
这样写起来很麻烦,如果有好多层转换A->B->C...的话。
有没有简化的写法?
======================================================================
#include
using namespace std;
class C {
int i;
public:
C(int j):i(j){}
void Print(){cout<<"C::i="< };
class B {
int i;
public:
B(int j):i(j){}
operator C()
{
return C(3*i);
}
void Print(){cout<<"B::i="< };
cla
c**********e
发帖数: 2007
11
来自主题: Programming版 - C++ Q04: template member
#include
using namespace std;
void f() { cout << "::f" << endl; }
struct P {
void f() { cout << "P::f" << endl; }
};
template
struct X : public T {
void g1() { f(); }
};
int main()
{
X

x;
x.g1();
return 0;
}
Choices
a. ::f
b. P::f
c. The program exhibits undefined behavior.

k*******d
发帖数: 701
12
来自主题: Programming版 - C++ Q07: unnamed namespace
经测试b是正确的
#include
namespace {int i;}
void f(){i++;
std::cout<<"i in f:"< }
int main(){
f();
i++;
std::cout<<"i in main:"< }
输出结果为
i in f:1
in in main:2
c**********e
发帖数: 2007
13
来自主题: 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
a****d
发帖数: 114
14
来自主题: Programming版 - 一个关于C++ template和overload的问题
下面这个程序为什么选择的是template version呢?
/******** 程序1 ********/
#include
using namespace std;
#include
using namespace std;
template
void f(const T a) {
cout<<"Template version."< }
void f(const int* a) {
cout<<"Plain version."< }
int main() {
int x=2;
int *a= &x;
f(a);
return 0;
}
/************************/
但是把所有的 int* 换成 int 之后选的是non-template version:
/********* 程序2 ************/
#include
using namespace std;
template
void f(const
r******9
发帖数: 129
15
来自主题: Programming版 - cin.clear() 怎么用
bool done = false;
int sel;
do{
cin >> sel;
if(cin.fail())
{
cout << "invalid input("< cin.clear();
}
else if(sel <0 || sel >= idx)
{
cout << "out of range [0-"<< idx-1 <<"], try again " << endl;
}
else
{
done = true;
}
}while(done == false);
如入 ‘a', 结果就死循环了, cin.clear()不是可以reset cin么?
N***m
发帖数: 4460
16
来自主题: Programming版 - 一个 default constructor 的问题
I tested using
#include
#include
using namespace std;
int main()
{
vector v(10);
for(int i=0;i<10;++i)
cout< cout< int j;
cout< return 0;
}
on gcc, the output is
0,0,0,0,0,0,0,0,0,0,
3650340
so it should be zeroed.
explicit vector ( size_type n, const T& value= T(), const Allocator& =
Allocator() );
on other compilers, I do not know.
w******7
发帖数: 24
17
来自主题: Programming版 - stl container erase in a loop
#include
#include
int main()
{
using namespace std;
deque que;
for(int i=0; i<10; ++i)
que.push_back(i);
deque::iterator iter = que.begin();
while(iter != que.end()) {
cout << *iter << endl;
++iter;
}
iter = que.begin();
while(iter != que.end())
{
cout << *iter << endl;
que.erase(iter);
}
return 0;
}
results:
0
1
2
3
4
5
6
7
8
9
0
0
1
2
3
4
5
6
7
8
9
Question: Why is there two "0" printed out when erasing? And if change
the c... 阅读全帖
p*u
发帖数: 2454
18
来自主题: Programming版 - how do I reseat a reference?
I know a reference in C++ cannot be reseated; am just trying to test
this concept. But it seems to me that as long as u wish to implement an
assignment operator u will not get a "reference reseating" compiler
error, it will just copy value of second object to first object. How can
I have a true compiler error?
Sample code:
"
#include
#include
int main()
{
std::vector v1( 10, 1 );
std::vector v2( 20, 2 );
std::vector& ref = v1;
for( int i = 0; i < re... 阅读全帖
c**********e
发帖数: 2007
19
来自主题: Programming版 - question on reserve() in vector container.
Code:
vector x;
x.reserve(3);
x[0]=5; // x.push_back(5);
cout << "x[0]=" << x[0] << endl;
x.reserve(10);
cout << "x[0]=" << x[0] << endl;
The output is x[0]=5 and x[0]=0. So it is not OK to assign vector values
with x[0]=5?
Using x.push_back(5), there is no such problem. I get x[0]=5 and x[0]=5. So
it is always safe to use push_back(). The question is, why x[0]=5 does not
work? Thanks.
f******y
发帖数: 2971
20
What compiler do you use? Can this compile?
cout << b[2].n < cout << c->n <
a****m
发帖数: 693
21
来自主题: Programming版 - 再请教一个class输出的问题
总是说不能display, 如果把所有的class里面的改成public, 就可以直接用num and
denum
输出到 display, 但是如果private,怎么都不行,谢谢。
#include
using namespace std;
class CFraction
{
private: //private method to get GCD
int num;
int denum;
public:
CFraction(int num, int denum);
int getnum();
int getdenum();
void display();
};
//constructor...
int CFraction::getnum(){
return num;
}
int CFraction::getdenum(){
return denum;
}
CFraction::CFraction( int a, int b )
{
num = a;
denum = b;
}
void CFraction::display() {
cout << ... 阅读全帖
z****e
发帖数: 2024
z****e
发帖数: 2024
23
来自主题: Programming版 - 关于const_cast,地址一样,值不同?
地址一样,值不同?
void f(){
const int i=1;
int* j=const_cast(&i);
*j=-1;
cout< cout<<&i<<" "< }
output:
1-1
0xbff30ff0 0xbff30ff0
s*w
发帖数: 729
24
来自主题: Programming版 - 对STL的set比较熟悉的进来看看
Google 了一下,这个问题没我想象的容易啊,大致是个 data view 的意思;好像有个
view
pattern 没明白啥意思;下面的是根据有人的建议的一个简单实现。主要麻烦是 set,
map 都不能
用 sort, 只能利用插入时候的缺省 sort
#include
#include
#include
#include
using namespace std;
int main()
{
map namescore;
multimap scorename;
namescore["tom"] = 100;
namescore["mary"] = 90;
namescore["rose"] = 95;
namescore["peter"] = 60;
for (map::iterator
it=namescore.begin();it!=namescore.end();it++) {
cout << it->first << ": ... 阅读全帖
r*******y
发帖数: 1081
25
来自主题: Programming版 - private destructor
Look at this example:
#include
using namespace std;
class A{
public:
virtual ~A(){cout <<"A"< };
class B:public A{
~B(){cout <<"B" < };
int main(){
A * pa = new B;
delete pa;
}
the output is
B
A
~B() is called because of the polymorphism. My question is that why
a private destructor ~B() can be called here?
Thanks
B*M
发帖数: 1340
26
来自主题: Programming版 - private destructor
是的,
class A{
public:
virtual void f(){
cout << "A's f" << endl;
}
};
class B: public A{
private:
void f(){
cout << "B's f" << endl;
}
};
int main(){
A* a = new B();
a->f();
delete a;
}
这段代码输出的是: B's f
public private的约束是在编译时候检查的,运行时就不管了,

time
r*******y
发帖数: 1081
27
it is interesting. I also write a simple code to check
#include
using namespace std;
class I{
public:
virtual void f(int i = 1){cout << i < };
class A: public I{
public:
void f(int j = 2){cout << j << endl;}
};
int main(){
I *p = new A;
p->f();
}
For this code, I think the result would have been 2
but in fact it is 1.
b********r
发帖数: 1080
28
来自主题: Programming版 - C++里用Blas/Lapack的问题 (转载)
【 以下文字转载自 Computation 讨论区 】
发信人: bankbuster (恭喜发财), 信区: Computation
标 题: C++里用Blas/Lapack的问题
发信站: BBS 未名空间站 (Tue Aug 9 14:57:49 2011, 美东)
我用atlas,程序里简单调用zgeev函数。编译没有错,没有警告。运行也不出错,但结
果完全不对。似乎函数根本没有被调用。
另外哪里能找到在C或者C++下调用Blas/Lapack函数的具体格式?我这里函数参数还是
在网上搜的。完全没有相关的手册。难道Blas/Lapack只是给fortran用的?
程序如下
#include
#include
#include
#include
#include
using namespace std;
typedef complex dcomplex;
extern "C" void zgeev_( char* jobvl, char* jobvr, int* n, ... 阅读全帖
J**********y
发帖数: 1891
29
有几个destructor呢?
我怎么老是数不对?谢谢。
// mytry1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
using namespace std;
class myclass
{
public:
int num;
myclass(int a)
{
num=a;
}
myclass(const myclass & a)
{
num=a.num;
cout << "copy constructor ... " << endl;
}

~myclass()
{
cout << "destructor ... " << endl;

}
};
int _tmain(int argc, _TCHAR* argv[])
{
myclass x(5);
... 阅读全帖
r*******y
发帖数: 1081
30
来自主题: Programming版 - member and friend
class A{
int i;
public:
A(int ii):i(ii){}
const A operator+(const A & a){
A b(i + a.i);
cout << "member"< return b;
}
friend const A operator +(const A &a, const A & b){
cout <<"friend"< return A(a.i + b.i);
}
};
int main(){
A a(1);
a + 1; // will call member operator +
}
This example shows that the member operator + will be called in
a + 1 a... 阅读全帖
a***y
发帖数: 2803
31
来自主题: Programming版 - member and friend
const A operator+(const A & a){}
的left operand不是const.
friend const A operator +(const A &a, const A & b){}
的left operand是const.
------------------------------------------------------
A a(1);
a + 1;
这个a不是一个const A,因为它本身的值可以变化,a.i可以从1变成2.所以compiler只会
用const A operator+(const A & a){}.
这个case不存在2个函数哪个优先用的問題.这个case的本质是left operand是不是const.
========================================
把main函数里的A a(1);变成const A a(1);编译,你会发现结果成了friend
int main(){
const A a(1);
a + 1;
}

class A{
... 阅读全帖
l*********s
发帖数: 5409
32
来自主题: 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(),... 阅读全帖
C***y
发帖数: 2546
33
【 以下文字转载自 JobHunting 讨论区 】
发信人: Chevy (Chevy), 信区: JobHunting
标 题: 问个C++ virtual function的问题
发信站: BBS 未名空间站 (Thu Oct 20 17:20:28 2011, 美东)
有没有办法强制基类中定义的pure virtual function在所有的继承类中实现
For example:
class A
{
public:
virtual void func() = 0;
};
class B: public A
{
public:
void func() { std::cout<<"In B"< };
class C: public B
{
public:
void func() { std::cout<<"In C"< };
class C中也必须实现 func,否则报错
c**********e
发帖数: 2007
34
来自主题: Programming版 - C++ Q97: reference and virtual (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q97: reference and virtual
发信站: BBS 未名空间站 (Fri Oct 21 19:58:51 2011, 美东)
What is the output of the following code? Why?
#include
using namespace std;
class student {
public:
virtual void sleep() { cerr << "student sleep" << endl; }
};
class grad: public student {
public:
virtual void sleep() { cerr << "grad sleep" << endl; }
};
void exam(student& stu) {
stu.sleep();
}
void main() {
grad g;
... 阅读全帖
c**********e
发帖数: 2007
35
【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q98: Call member function in virtual function
发信站: BBS 未名空间站 (Fri Oct 21 20:04:02 2011, 美东)
What is the output of the following code? Why?
#include
using namespace std;
class base {
public:
void pay() { cout << "Base::pay" << endl; }
virtual void eat() { pay(); }
};
class derived: public base {
public:
void pay() { cout << "Derived::pay" << endl; }
};
void main() {
base* p = new derived;
... 阅读全帖
j*****u
发帖数: 186
36
下面程序来自于essential C++ 3.6节,给定一个数组,输出比某个数小的所以元素。
main函数中用了back_inserter,按道理是不是应该include iterator头文件和
using std::back_inserter? 但是为什么把这两句注释掉程序也能跑呢?谢谢。
===================================================================
#include
#include
#include
#include
#include
//#include
using std::vector;
using std::cout;
using std::endl;
using std::less;
//using std::back_inserter;
template 阅读全帖
j*****u
发帖数: 186
37
下面程序来自于essential C++ 3.6节,给定一个数组,输出比某个数小的所以元素。
main函数中用了back_inserter,按道理是不是应该include iterator头文件和
using std::back_inserter? 但是为什么把这两句注释掉程序也能跑呢?谢谢。
===================================================================
#include
#include
#include
#include
#include
//#include
using std::vector;
using std::cout;
using std::endl;
using std::less;
//using std::back_inserter;
template 阅读全帖
c**********e
发帖数: 2007
38
The following two piece of codes do the same. What are their differences? Is
the second one look more professional, or from a more experienced prorammer?
vector nodes;
... ...
for (int i=0;i cout << nodes[i]->toString() << endl;
}
vector nodes;
... ...
for (vector::iterator iter = nodes.begin();
iter != nodes.end(); iter++) {
cout << (*iter)->toString() << endl;
}
A**u
发帖数: 2458
39
来自主题: Programming版 - namespace 问题
#include
#include
using namespace std;
namespace S{
class A{
};
void swap(const A& x, const A& y){
std::cout << "A swap" << std::endl;
}
}
int main()
{
S::A x;
S::A y;
swap(x,y);
//S::swap(x,y);
std::cout << "fishing" << std::endl;
return 0;
}
这个code里, namespace S里的 swap 比 std里的swap specific
为啥 gcc 编译后,调用std版本呢
x******a
发帖数: 6336
40
来自主题: Programming版 - 请教一个monte carlo问题
求$\int_0^1e^{x^2} \,dx$, 为什么下面这段code总是返回1?谢谢
#include
#include
using namespace std;
double uniRand()
{
return double(rand()/RAND_MAX);
}
int main()
{
srand((unsigned)time(0));
//number of draws;

int N;
cout<<"enter N: "< cin>>N;
double result=0;
//integrand

for(int i=0; i< N; i++)
{
double temp=pow(uniRand(),2);
re... 阅读全帖
h**l
发帖数: 168
41
来自主题: Programming版 - function call?
The following code with function call can shift a string but the code
without function call can not. What might be the problem? Thanks!
_______________________________________________________
void shift(char a[])
{
int i=0;
while(a[i]) a[i] = a[++i];
}
int main()
{
char str[]= "afdd";
shift(str);
cout< return 0;
}
_____________________________________________________________
int main()
{
char str[]= "afdd";
int i=0;
while(str[i]) str[i] ... 阅读全帖
g***l
发帖数: 2753
42
下面这段代码中,为什么不能用static_cast把一个(float*)的指针强制转换成
(int*),而用 reinterpret_cast? 请问这是规定吗? 假设 sizeof(float) ==
sizeof(int).
[localhost]$ g++ -g sample.cpp -o sample
sample.cpp: In function ‘int main()’:
sample.cpp:13: error: invalid static_cast from type ‘float*’ to type ‘int
*’
在C中,可以这么转,gcc也会发warning,但是不会编译失败。
谢谢了。
1
2 #include
3 using namespace std;
4 int main()
5 {
6 int inumber=1000;
7 float fnumber=1234.56;
8
9 int* piaddr = &inumber;
10 float* pfa... 阅读全帖
l********a
发帖数: 1154
43
测试没问题啊,把字符串里面的\改成\\
测试代码
void dos2u(string& path)
{
string newpath = path+"abcd";
cout << newpath.c_str() << endl;
}
int main()
{
string path("c:\\test");
cout << path.c_str() << endl;
dos2u(path);
return 0;
}
k*******3
发帖数: 1909
44
来自主题: 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... 阅读全帖
c*********e
发帖数: 16335
45
来自主题: Programming版 - 帮忙看一个code
我在main函数里每行之间加了一行cout<<"this is line x"< 什么输出都没有。
从来没见过这种情况。compile通过,但是run的时候什么都没有,连cout<<...< 会输出的都没有出现。
r**a
发帖数: 536
46
来自主题: Programming版 - 帮忙看一个code
我试了试下面两个code,第一个比我原来贴的那个速度明显快了但是还是比不上第二个。
Code1:
#include
#include
using namespace std;
class A
{
public:
A(const size_t N): _N(N) {}
vector > getXY()
{
//vector xytemp;
//for (size_t i = 0; i != 2; ++i)
// xytemp.push_back(i+3);
vector > xy(_N);
for (size_t i = 0; i != _N; ++i)
for (size_t j = 0; j != 2; ++j)
xy[i].push_back(j+3... 阅读全帖
i*********n
发帖数: 58
47
Template function with scalar argument.
Compiler will optimize away the flag.
#include
using namespace std;
#define foo_plus foo
#define foo_minus foo
template
int foo(int a, int b) {
return plus ? (a + b) : (a - b);
}
int
main( int args, char *argv[] )
{
cout << foo_plus(5, 2) << endl;
cout << foo_minus(5, 2) << endl;
return 0;
}
xt
发帖数: 17532
48
来自主题: Programming版 - Exception 问题求助
我在一个class里面写了一个方法:
class MyException :public std::exception
{
public:
MyException(const int code);
int getCode();
private:
int _code;
}
Class A
{
public:
void doWork() throw(MyException);
...
};
在一个方法里调用:
...
A a;
try {
a.doWork();
}catch(MyException &e) {
cout << "Code=" << e.getCode << endl;
} catch (...) {
cout << "An error occurred" << endl;
}
结果:每次总是落到catch(...)那里面。
谁知道为什么?我用VC的release编译好像有这个问题
H****n
发帖数: 26
49
如题,请大师们帮帮忙,编译器现在主要报错在静态变量打初始化部分,我百思不得其
姐。
#include
using namespace std;
class link{
private:
string ss;
link *prev;
link *next;
static int count; //the counter to show how many nodes
int pos; //the position to insert node
static link* fp; //a temp node for insertion
static link* head;
static link* tail;
public:
link(string s1="", int i=0):ss(s1),pos(i){
if(head==NULL&&tail==NULL){
... 阅读全帖
z********i
发帖数: 568
50
来自主题: Programming版 - C++ Segment Fault
char* newStr=function_call(...)
cout<<"newStr="< cout<<"newStr="<
首页 上页 1 2 3 4 5 6 7 8 9 10 (共10页)