h****b 发帖数: 157 | 1 简单的不好意思问,谢了
int word = 0x1;
char *byte = (char *) &word;
cout<
printf("%d\n", byte[0]);
为啥cout 和 printf 打出来的不一样? printf的对,cout是乱码 |
|
s*******e 发帖数: 664 | 2 ☆─────────────────────────────────────☆
GodBlessMe (GodBlessMe) 于 (Sat Nov 7 17:30:51 2009, 美东) 提到:
void f1(char*& p)
{
p="a";
}
void f2(char *& p)
{
*p='a';
}
int main()
{
char s[]="1234";
char *p=s;
f2(p);
cout<
f1(p);
cout<
cout<
return 0;
}
a234
a234
a
我没有闹明白为什么为什么char*&调用的时候
能对字符修改,却不能赋予新的字符串呢?
我发现f1赋值后;p的reference地址就变了呢
我翻了基本书,也没找到答案。哪位帮解释解释
☆─────────────────────────────────────☆
GodBlessMe (GodBlessMe) 于 (Sat Nov 7 17:34:23 2009, 美东) 提到:
还有f1(s)也是编译 |
|
z****e 发帖数: 2024 | 3 我写了一个,运行貌似可以。
但是有一个极其诡异的bug
在 longest_mono_seq ::operator() 里面,如果把开头的四个打印语句去掉,运行结
果完全错误。
可是那几个打印是take const的,不会修改的呀?
很奇怪。我也解释不了。
#include
#include
#include
#include
using namespace std;
template//a template to print container
void prints(const Con& c, string name){
cout<
typename Con::const_iterator it=c.begin();
for(;it!=c.end();++it){
cout<<*it<<" ";
}
cout<
}
class longest_mono_seq{//the core function object |
|
c**********e 发帖数: 2007 | 4 #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 |
|
r******s 发帖数: 925 | 5 The code in below doesn't work. What's the best way to fix it without
changing define of A, B, C?
what's the output?
#include
#include
using namespace std;
typedef char TT[10];
int B (TT &b)
{
cout << b << ", Len: " << strlen(b)<
}
int A (TT a)
{
cout << a << ", Len a : " << strlen(a) << endl;
B(a);
}
int C (char c[10])
{
cout << c << ", Len c : " << strlen(c) << endl;
}
int main()
{
TT d;
cin >> d;
A(c);
C(d);
return 0;
} |
|
N***m 发帖数: 4460 | 6 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. |
|
C**m 发帖数: 126 | 7 #include
#include
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
int main(int argc, char* argv[])
{
X *p;
std::cout << "the address " << (void *) p->get_X() << std::endl;
X a;
std::cout << "the address " << (void *) a.get_X() << std::endl;;
X b;
std::cout << "the address " << (void *) b.get_X() << std::endl;;
return 0;
}
/... 阅读全帖 |
|
C**m 发帖数: 126 | 8 #include
#include
class X {
public: X* get_X();
private:
static X _x;
};
X X::_x;
X* X::get_X()
{
return &_x;
}
int main(int argc, char* argv[])
{
X *p;
std::cout << "the address " << (void *) p->get_X() << std::endl;
X a;
std::cout << "the address " << (void *) a.get_X() << std::endl;;
X b;
std::cout << "the address " << (void *) b.get_X() << std::endl;;
return 0;
}
/... 阅读全帖 |
|
e******0 发帖数: 211 | 9 在看c++ faq++,有个问题
faq 31.04
#include
#include
using namespace std;
class X { };
void mayThrow() throw(int)
{ throw 42; }
class Fred {
public:
Fred() throw(bad_alloc, int);
~Fred() throw();
Fred(const Fred& f) throw();
Fred& operator= (const Fred& f) throw();
private:
X* p_;
};
Fred::Fred() throw(bad_alloc, int)
: p_(new X()) { mayThrow(); }
Fred::~Fred() throw()
{ cout << "Not reached #1\n"; delete p_; }
int main()
{
try {
Fred f;
cout << "Not reached #2\n... 阅读全帖 |
|
x******g 发帖数: 41 | 10 void testAddressOf()
{
int a = 4;
int *b = &a;
int c = 5;
cout << "address of a: " << &a << endl;
cout << "address of &b " << &b << endl;
cout << "address of c: " << &c << endl;
}
三个地址分别是
0015F75C
0015F750
0015F744
整形不是4个字节么,为什么地址之间差距是0xC=12呢? |
|
H****r 发帖数: 2801 | 11
这个 "C++ temporary object can not be bound to non-const reference" 俺似乎理解
了,但是楼主的例子在VS2010 下并没有报错啊?
class M{
public:
int i;
public:
M(){ std::cout << "default constructor" << std::endl;}
M(int ii):i(ii){ std::cout << "constructor"<< std::endl;}
M(M & mm):i(mm.i){ std::cout << "copy constructor"<< std::endl;}
};
int main()
{
M mm = 1;
}
其中 M(M & mm):i(mm.i) 改成 M(const M & mm):i(mm.i) 并没发现对 M mm = 1; 有
任何影
响啊。 |
|
H****r 发帖数: 2801 | 12
甚至写成这样都可以编译运行啊(VS 2010):
#include
class M{
public:
int i;
public:
M(){ std::cout << "default constructor" << std::endl;}
M(int ii):i(ii){ std::cout << "constructor"<< std::endl;}
M(M& mm):i(mm.i){ std::cout << "copy constructor"<< std::endl;}
};
int main()
{
M mm(M(1));
} |
|
g*********s 发帖数: 1782 | 13 how does the compiler know if the 1st get() or the 2nd get() should be
called?
#include
using namespace std;
class X {
public:
X(int x = 0): dat(x) {}
void print() const { cout << dat << endl; }
void inc() { dat ++; }
private:
int dat;
};
class Y {
public:
Y(const X& x): dat(x) {}
Y(const Y& y) { dat = y.get(); }
X& get() { cout << "ref" <
const X& get() const { cout << "const ref" << endl; return dat;
}
private:
X dat;
};
int main()
{
X x(10);
Y y(x);
Y y2(y);
y.g... 阅读全帖 |
|
a***y 发帖数: 2803 | 14 #include
using namespace std;
int main() {
int a[5];
a[0]=9;
a[1]=8;
a[2]=7;
a[3]=6;
a[4]='\0';
int i=1;
a[i++]=i;
a[i]=i++;
cout << "a[0] is "<< a[0] << endl;
cout << "a[1] is "<< a[1] << endl;
cout << "a[2] is "<< a[2] << endl;
return 0;
}
运行结果是
a[0] is 9
a[1] is 1
a[2] is 2
a[i++]=i;语法正确,而且运行也正确啊.
m
e
undefined |
|
j*****u 发帖数: 186 | 15 下面程序来自于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 | 16 下面程序来自于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 阅读全帖 |
|
g*****1 发帖数: 998 | 17 有个几万行的文件,主要有个循环一行行的读,然后计算一个数。
程序结尾cout这个数,我也加了cin.get().
我直接运行时,程序没有任何反应(抬头不显示running),没报错,控制台停在那,不显示结果,随便敲键控制台也不消失。
然后我在每个loop里cout一下,然后运行,这时候控制台就出来不停的输出每个loop里cout的内容,然后最后输出了我要的结果,也停住了
我知道我表达清楚了没有,请问错在哪? |
|
N**********d 发帖数: 9292 | 18 源程序是:
#include
#include
using namespace std;
int main(int argc, char * argv[])
{
std::string m_ColumnName [] =
{
"str1",
"str2"
"last_one"
};
cout << m_ColumnName[0].substr(0,4) << endl;
cout << m_ColumnName[1].substr(0,4) << endl;
cout << m_ColumnName[2].substr(0,4) << endl;
return 0;
}
赋值的时候,"str2"后面少了个逗号,然后"last_one"到哪里去了?
是不是g++直接就把它扔了?
前两行输出都是预期的
str1
str2
而第三行则不确定,经常产... 阅读全帖 |
|
A**u 发帖数: 2458 | 19 照书编了一个,
结果总是不对,请大牛看看问题出在哪里呢
#include
#include
#include
pthread_mutex_t region_mutex = PTHREAD_MUTEX_INITIALIZER;
sem_t items;
int b; /* buffer size = 1; */
void add_buffer(int i){
b = i;
}
int get_buffer(){
return b ;
}
void *producer(void*)
{
int i = 0;
std::cout <<"I'm a producer" << std::endl;
while (1) {
pthread_mutex_lock(®ion_mutex);
add_buffer(i);
pthread_mutex_unlock(®ion_mutex);
sem_post(&ite... 阅读全帖 |
|
x******a 发帖数: 6336 | 20 抄了cplusplus上面的一个小程序,g++是提示下面的错误,请问怎么回事?谢谢
min.cpp:2:2: error: invalid preprocessing directive #inlcude
name's-MacBook-Pro:~ name$ emacs min.cpp
#include
#inlcude
using namespace std;
int main()
{
cout<<"min(1,2)"<< min(1,2)<
cout <<"min(1, 3.5)"<< min(1.0, 3.5)<
cout<< "min(abcd, adccd)"<< min('a', 'b')<
return 0;
} |
|
c**********e 发帖数: 2007 | 21 struct Cat {
char * name;
Cat(){cout << "made cat\n";}
Cat(const Cat& other){cout << "copy cat\n";}
~Cat(){cout << "gone";}
};
int main(){
Cat * cp = new Cat();
Cat c1;
Cat c2;
c2 = c1;
Cat c3 = c2;
Cat c4(c2);
return 0;
} |
|
m**o 发帖数: 5261 | 22 I am not very familiar with c++. I want to print out an integer matrix line
by line. Why the cout<
void Matrix::printMatrix(){
for(unsigned int i=0; i
{
Matrix::printRow(i);
}
}
void Matrix::printRow(unsigned int rownumber)
{
matrix_row_t row=grid.at(rownumber);
for(unsigned int i=0; i < row.size(); i++)
{
cout<
}
cout<
} |
|
X*4 发帖数: 101 | 23 关于boost shared_ptr的,
#include
#include
#include
class A{
public:
virtual void sing() = 0;
protected:
virtual ~A(){
std::cout << "dtor of base" << std::endl;
}
};
class B: public A{
public:
virtual void sing()
{
std::cout << "DO re mi fa so la" << std::endl;
}
~B(){
std::cout << "Dtor of Derived " << std::endl;
}
};
boost::shared_ptr create(){
boost::shared_ptr p(new B());
... 阅读全帖 |
|
g***l 发帖数: 2753 | 24 指向子类对象的基类指针能访问子类中重载的虚函数,而不能访问其私有函数。
比如
class CBase
{
public:
CBase();
virtual void Print(){cout<<"base"<
};
class CDerived: public CBase
{
public:
CDerived();
virtual void Print(){cout<<"Derived"<
void Personal(){cout<<"Derived private owned"<
}
1. int main()
2. {
3. CDerived oderived;
4. CBase* pbase=&oderived;
5. pbase->Print();
6. pbase->Personal();
7. }
编译的时间,error: ‘class CBase’ has no member named ‘Personal’in line 6.
我的问题是,
1. 如果是因为pbas... 阅读全帖 |
|
e********r 发帖数: 2352 | 25 为了使用try ... throw ... catch的方式处理异常,写了以下一段程序,就是读取一
个文件
ifstream file;
file.exceptions(ifstream::failbit | ifstream::badbit);
try{
file.open("./file");
string str;
while(getline(file, str))
{
cout<
}
}
catch(ifstream::failure e)
{
cout<<"Return information: "<
}
file.close();
请问为什么总是会执行catch... 阅读全帖 |
|
n***s 发帖数: 287 | 26 我需要写个简单的C++程序test.cpp, 如
int main() {
int a;
double b;
string c;
// do something about a, b, c
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
比如编译之后的executable叫test.out,
然后需要在一个bash script中运行test.out, 我想在bash中定义
三个变量A, B, C, 运行完test.out之后就立即把abc的值赋给ABC,
两个问题:
how to make variable B store a floating point number ??
how to pass values of a,b,c to A,B,C in bash ??
我bash比较外行,不知道这个该怎么做?或者cpp程序也要做相应
调整?还请高人指点,包子答谢! |
|
b*****n 发帖数: 2324 | 27 std::cout << "3.5 map1.size(): " << map1.size() << endl;
std::cout << "4 map1[key1].size(): " << map1[key1].size() <<
endl;
std::cout << "4.5 map1.size(): " << map1.size() << endl;
output:
3.5 map1.size(): 0
4 map1[key1].size(): 0
4.5 map1.size(): 1 |
|
l********a 发帖数: 1154 | 28 不太了解你的需求
如果只是针对int类型有特殊操作,其他类型都调用模板函数,你可以明确对于int的set
函数(即使有同名template set() 存在),我目前项目有个类似的情况就
是这样解决的.
网上说这跟c++的函数名称搜索次序有关,我忘记在哪里看到的了
测试代码:
void set(int t)
{
std::cout << "print from normal function 1\n" << t << std::endl;
}
void set(string t)
{
std::cout << "print from normal function 2\n" << t.c_str() << std::endl;
}
template
void set(T t)
{
std::cout << "print from template function\n" << t << std::endl;
}
int main()
{
int it = 10;
float ft = 10.0;
double dt = 20.53;
... 阅读全帖 |
|
S**I 发帖数: 15689 | 29 littlebirds' answer is relevant but not 100% correct. To be exact, the
second parameter of overloaded operator << is a reference to str type, which
requires it to be a l-value. s1 + s2, or (s1 + s2) (parentheses or not doesn
't matter here), is a r-value, so compiler cannot find a match for std::cout
<< s1 + s2. If you change the parameter type to const reference to str, a r
-value is acceptable and std::cout << s1 + s2 works fine.
In fact, if you change the second parameter to pass by value (st... 阅读全帖 |
|
y**b 发帖数: 10166 | 30 linux下面我直接用ps或top命令查看的,可以清楚看到各个进程的内存消耗。
swap这个方法我用google搜索到的,似乎是通用的一个trick; effective stl里面也有
提到。
下面是源码,只有swap之后进程的内存消耗才降到近乎0(getchar的时候察看)。
#include
#include
#include
int main () {
std::vector vec;
vec.resize(10000000,-1);
std::cout << "resize finished." << std::endl;
getchar();
vec.clear();
std::cout << "clear finished." << std::endl;
getchar();
std::vector().swap(vec);
std::cout << "swap finished." << std::endl;
getchar();
ret... 阅读全帖 |
|
s*w 发帖数: 729 | 31 #include
using namespace std;
struct node {
int val;
node* next;
node(int v):val(v),next(NULL) {}
};
struct sll {
node* head;
sll():head(NULL) {}
~sll() {
cout << "sll destructing starts here" << endl;
node* p = head;
while(p) { // i am having problems destrucing here with looped list
node* todel = p;
p = p->next;
delete todel; // i thought delete make todel NULL so it breaks loop
todel = NULL; // does this help? nono... 阅读全帖 |
|
s****l 发帖数: 41 | 32 #include
class A
{
public:
A(int n = 2) : m_i(n) { }
~A() { std::cout << m_i; }
protected:
int m_i;
};
class B
: public A
{
public:
B(int n) : m_x(m_i + 1) , m_a(n) { }
public:
~B()
{
std::cout << m_i;
--m_i;
}
private:
A m_x;
A m_a;
};
int main()
{
{ B b(5); }
std::cout << std::endl;
return 0;
}
为什么输出为 2531? |
|
x**********d 发帖数: 693 | 33 类似C++里面的这段请问用python如何实现?
for (int i=0; i<5; i++){
a=a+i;
cout<
for (int j=0; j<5; j++{
b=b+j;
cout<
}
cout<
} |
|
m********r 发帖数: 334 | 34 class example {
public:
example(string str){
cout <<"ctor with string "<
example() {
cout<<"ctor without string" <
example( example& rhs){
cout<<"copy ctor "<
};
int main (int argc, char* argv[])
{
string str = "123456789";
example e1(str);
example e2 = str;
example e3 = e1;
example e4 = example();
return 0;
}
这个例子来自C++Primer,在VS2010可以编译,gcc下e2 and e4报错:no matching
function for call to example::example(example)
如果在copy constr... 阅读全帖 |
|
a**e 发帖数: 64 | 35 有一点很有意思,虽然编译的时候要求有copy或者move constructor,但是实际上并不
一定会使用。比如运行下面的例子:
class Person {
public:
Person(string name): name_(name)
{ cout << "in normal constructor" << endl; }
Person(const Person&)
{ cout << "in copy constructor" << endl; }
Person(Person&&)
{ cout << "in move constructor" << endl; }
private:
string name_;
};
int main()
{
vector persons;
persons.emplace_back("George");
};
output:
in... 阅读全帖 |
|
vi 发帖数: 309 | 36 奇怪的问题,谁帮忙看一下,谢谢!
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
list tlist(arr, arr+(sizeof(arr)/sizeof(int)));
int psum;
for_each(tlist.begin(), tlist.end(), [&psum](int &_val) {
_val += psum;
psum = _val;
});
for (auto _val: tlist) { cout << _val << " "; };
cout << endl;
cout << *(tlist.begin());
}
g++ default:
$2 4 7 11 16 22 29 37 46
2$
if compile with -O1 then it will produce the correct output:
$1 3 6 10 15 21 28 36 45
1$ |
|
x******a 发帖数: 6336 | 37 void testingSharedPtr1(){
boost::shared_ptr > v1(new std::vector(100));
std::cout<<(*v1)[0]<<"n";
boost::shared_ptr > v2=v1;
v2->push_back(9.0);
std::cout<<(*v1).size()<<"n";
std::cout<<(*v1)[100]<<"n";
}; |
|
d****i 发帖数: 4809 | 38 The principle of polymorphism is the same. Consider the following equivalent
C++ code, it yields the same results. You can even better change to: Base *
a = new Derived(); and the results are the same.
#include
using namespace std;
class Base {
private:
int x;
public:
Base() : x(1) {}
virtual ~Base() {};
void showX1() {
cout<
}
virtual void showX2() {
cout<
}
};
class Derived : public Base {
private:
int x;
public:
... 阅读全帖 |
|
k**********g 发帖数: 989 | 39
Wrong code, not sure why it pass compilation and/or doesn't crash. Doesn't
compile on my machine.
If you want them to point to strings, use
int main()
{
const char* p[3] = { "a", "b", "c" };
std::cout << p[0] << std::endl;
std::cout << p[1] << std::endl;
std::cout << p[2] << std::endl;
return 0;
}
Notice double quote, because each is a string literal. |
|
Q**g 发帖数: 183 | 40 像是经典的Double dispatch pattern.
你大概是想 Dog process DogData, Cat process CatData; 当 Dog 遇上 CatData 或者
Cat 遇上 DogData 就用缺省的 Animal process AnimalData。试试
#include
class Animal;
class Data;
class Cat;
class CatData;
class Dog;
class DogData;
using namespace std;
class Data {
public:
virtual void process(const Animal* animal) const {
cout << "Default Data and Animal" << endl;
}
virtual void process(const Cat* animal) const {
process((const Animal*) animal);
}
... 阅读全帖 |
|
K***a 发帖数: 497 | 41 function(vector a)
{
vector::iterator ater = a.begin();
vector::iterator bter = a.begin();
cout <<*ater <
cout <<*(ater+1) <
for(bter; bter != a.end; bter++)
{
cout <<*bter;
}
}
调用function两次。在两次调用中,ater都能输出vector a中每个数据。但是bter只有
在第一次调用中能输出vector a... 阅读全帖 |
|
m********5 发帖数: 17667 | 42 why you use iss.eof()?
The common way to do this is directly check after iss>>temp, because their
can be fail, bad, whitespace, or eof. If either fail/bad signal is True, the
temp is not valid.
###
void test(){
string s = "A B C "; // output is A B C C
// If string s = "A B C", then the output would be A B C
istringstream iss(s);
string temp;
int _i = 0;
while (!iss.eof())
{
cout << _i << " iss stat[good,bad,fail,eof]: [" << iss.good() << iss
.bad() <阅读全帖 |
|
t********e 发帖数: 1169 | 43 【 以下文字转载自 JobHunting 讨论区 】
发信人: mitbbs59 (bEQi), 信区: JobHunting
标 题: 本版1年以内的所有 面经题目,含帖子link [为大家方便]
发信站: BBS 未名空间站 (Fri Jan 29 14:20:44 2010, 美东)
不敢保证全部涵盖,大部分的都在。
我自己找了一遍,大家一起用着都方便。
不过只是含有题目的帖子 我才包含进来了,只分享经验没贴题目的 我都没有包含
进来。
大家复习着方便。
1. 一个sorted interger Array[1...N], 已知范围 1...N+1. 已知一个数字missing。
找该数字。
把原题改为unsorted,找missing数字。 performance。
2. 复制linked list。 已知每个节点有两个pointer,一个指向后一个节点,另一个指向
其他任意一节点。 O(n)时间内,无附加内存,复制该linked list。(存储不连续)
3. 一个party N个人,如果一个人不认识任何其他人,又被任何其他人认识,此人为
celeb... 阅读全帖 |
|
t********e 发帖数: 1169 | 44 【 以下文字转载自 JobHunting 讨论区 】
发信人: mitbbs59 (bEQi), 信区: JobHunting
标 题: 本版1年以内的所有 面经题目,含帖子link [为大家方便]
发信站: BBS 未名空间站 (Fri Jan 29 14:20:44 2010, 美东)
不敢保证全部涵盖,大部分的都在。
我自己找了一遍,大家一起用着都方便。
不过只是含有题目的帖子 我才包含进来了,只分享经验没贴题目的 我都没有包含
进来。
大家复习着方便。
1. 一个sorted interger Array[1...N], 已知范围 1...N+1. 已知一个数字missing。
找该数字。
把原题改为unsorted,找missing数字。 performance。
2. 复制linked list。 已知每个节点有两个pointer,一个指向后一个节点,另一个指向
其他任意一节点。 O(n)时间内,无附加内存,复制该linked list。(存储不连续)
3. 一个party N个人,如果一个人不认识任何其他人,又被任何其他人认识,此人为
celeb... 阅读全帖 |
|
r*****t 发帖数: 286 | 45 ☆─────────────────────────────────────☆
ggyygs (ggyygs) 于 (Thu Jun 7 11:36:17 2007) 提到:
其中加???那两行有啥用啊.直接用pp[i]=new int [n];不就可以了吗?
int **pp;
int m,n;
cout<<"m= "<
cin>>m;
cout<<"n= "<
cin>>n;
pp=new int *[m];
for(int i=0;i
??? if((pp[i]=new int [n])==NULL)
??? exit(0)
cout<<"input values"<
for(i=0;i
{
for(int j=0;j
cin>>pp[i][j];
}
for(i=0;i
{
for( |
|
w*******x 发帖数: 489 | 46 当场想晕了,只好给了个Nlog(N)算法。
哎。写写才记得。O(N):
#include
using namespace std;
int main(int argc, char *argv[])
{
int x[20];
for(int i=0;i<20;i++)
x[i]=rand()%100-50;
int i=0;
int j=0;
int im=0;
int jm=0;
int max=-1;
int sum=0;
//最大求和子序列
for(j=0;j<20;j++)
{
sum+=x[j];
if(sum>max){max=sum;im=i;jm=j;}
if(sum<0){i=j+1;sum=0;}
}
for(int i=0;i<20;i++)cout<
cout<<"max="<阅读全帖 |
|
O******5 发帖数: 483 | 47 #include
int main(int argc, char * argv[]) {
int mit_sb = 250;
std::cout << mit_sb + 53 << "\n";
std::cout << mit_sb * 2 << "\n";
return 0;
}
|
|
P**T 发帖数: 2274 | 48 iostream的library 可以召唤 using namespace std;
这样就可以直接用cout而不是std::cout了 |
|
v*****t 发帖数: 127 | 49 然后在贴一个decorator的做一个比较:
class Fur{
public:
virtual void describeself()=0;
};
class Table:public Fur{
public:
void describeself(){cout<<"I'm a table";}
};
class Material:public Fur{
public:
virtual void describeself()=0;
protected:
Fur *fur;
string material_name;
};
class Wood: public Material{
public:
Wood(Fur *f){fur = f; material_name = "wood";}
void describeself(){
fur->describeself();
cout<<" with "<
}
};
class Steel: publi |
|
s*****r 发帖数: 773 | 50 看了网上的讨论, 关于amazon那道wood steel table chair furniture的题目
自己写了一个, 牛人帮我看看我是否写的有问题......
在我机器上编译通过, 运行成功.
#include
using namespace std;
class stuff {
public:
stuff() {}
virtual ~stuff() {}
virtual void info() = 0 ;
};
class table : public stuff {
public:
table() {}
~table() {}
void info() { cout<<"Table "<
};
class chair : public stuff {
public:
chair(){}
~chair(){}
void info() {cout<<"Chair"< |
|