e*******i 发帖数: 56 | 1 No map is needed. Source is as following:
///////////////
#include
using namespace std;
int findLongest(char *s, char **start)
{
if(!s) return 0;
char first, second;
char *beginOfPreviousChar;
int currMax=0;
char *curr=s;
first=*s;
*start=s;
while(*curr)
{
if(*curr!=first) break;
currMax++;
curr++;
}
if( !*curr ) return currMax; //end of string
else
{
second=*curr;
beginOfPreviousChar=curr... 阅读全帖 |
|
r********d 发帖数: 7742 | 2 lz, 三妹给的答案肯定是不work的。
不过既然三妹是无法改变的,我们还是从提高自身身上入手。
fib这个东西确实比较基础,但是有很多种玩法。这些玩法玩出来了不算什么,可一旦
玩不出来的话会比较伤感情。
我写一个你看看work不,这个应该没有重复打印和计算。。。N不能太大。
fib增长是1.6^n。用int64_t或者long long能多撑一会儿。。。
#include
using std::cout;
void PrintFib(int n, int n1, int n2) {
//handles the missing heading 1
if (0 == n1)
cout << 1 << '\t';
if (n > 0) {
int sum = n1+n2;
cout << sum << '\t';
PrintFib(n-1, n2, sum);
}
}
int main() {
const int MAXN = 20;
int a = 0, b = 1;
PrintFib(MAXN, a, b... 阅读全帖 |
|
s*********d 发帖数: 2406 | 3 更新
public boolean findword(String randomletter, String magazine) {
HashMap magaword=new HashMap() ;
String[] words=magazine.split("\ ") ;
for (String word:words){
if (!magaword.containsKey(word)){
magaword.put(word,1) ;
} else {
int cout= magaword.get(word);
cout++ ;
magaword.put(word, cout) ;
}
}
String[] ... 阅读全帖 |
|
u*****o 发帖数: 1224 | 4 和大家分享网上看来的最短解,C++的,竟然一个IF也没用
#include
int main(){
for(int i = 1 ; i <= 100 ; ++i) ( (i%5)? ((i%3)?(cout << i):(cout << "
no")): cout << ((i%3)? "yes": "yes and no")) << endl;
} |
|
g***9 发帖数: 159 | 5 贴个DP的完整代码给大家讨论,测了几个case感觉是对的解法,简单原理:
假设a1,a2 ... an 和 b1,b2...bn 都在 1.. n之间,便于讨论
table[i][j] 表示了只包含从 a1..ai 到 b1..bj 作为桥端点的结果
rev 是逆向映射。
DP是假设了ai bj是一一对应,不知道有重复映射的话,怎么做比较好?
有么有人能贴个排序的代码?谢谢
#include
#include |
|
s*****b 发帖数: 8 | 6 我来贴一个。
Rocket fule (Software Engineer - Machine Learning Scientist ) 技术电面后code
test. code通过了所有test cases. 人家看过code 后就拒了。问题在哪里呢?请各位
牛人不吝赐教。题目本版以前贴过
You are standing in a rectangular room and are about to fire a laser toward
the east wall. Inside the room a certain number of prisms have been placed.
They will alter the direction of the laser beam if it hits them. There
are north-facing, east-facing, west-facing, and south-facing prisms. If the
laser beam strikes an east-facing prism, its cours... 阅读全帖 |
|
s********u 发帖数: 1109 | 7 为了保险起见,我自己试了一下:
stringstream stream;
stream.str("3745+38");
int num;
char op;
stream>>num;
cout << num << endl;
stream>>op;
cout<< op <
stream>>num;
cout<
确认是可以分开3745,+, 38。就算没有空格也是如此。
何况应该后缀表达式数字之间至少是有空格隔开的,否则怎么表示23+3.
在这种情况下,stream>>str,然后再用string创建stream转换成数字或者直接取str[0
]操作符就行了。 |
|
s******d 发帖数: 424 | 8 5个里面选三个
#include
#include
#include
#include
#include
int main()
{
int values[] = { 1, 2, 3, 4, 5};
int elements[] = { 1, 1, 1, 0, 0};
const size_t N = sizeof(elements)/sizeof(elements[0]);
assert(N == sizeof(values)/sizeof(values[0]));
std::vector selectors(elements, elements + N);
int count = 0;
do
{
std::cout << ++count << ": ";
for (size_t i = 0; i < selectors.size(); ++i)
{
if (selectors[i])
{
... 阅读全帖 |
|
w********s 发帖数: 1570 | 9 来自主题: JobHunting版 - 狗狗家面筋 switch基本上就是
CMP XXX, VALUE
JUMP XXXXXX
举个例子,
void dummy(int s)
{
switch (s)
{
case 0:
std::cout << "this is 0.";
break;
case 1:
std::cout << "this is 1.";
break;
default:
std::cout << "default.";
break;
}
}
步骤:
parameter s(SS:[EBP+8]) -> EAX
move EAX to the stack
compare the value (s, which is in SS:[EBP-0F4] to switch values 0)
jump if equal (the code for handling case 0)
compare the value to 1
jump if equal
if there is no ... 阅读全帖 |
|
f**********t 发帖数: 1001 | 10 // 对于参数n,输出从0到n之间所有含5的数字。
void print5_(unsigned up, unsigned prefix, unsigned numLen, bool has5) {
if (numLen == 1) {
if (has5) {
for (unsigned i = 0; i < 10; ++i) {
if (prefix * 10 + i > up) {
return;
}
cout << prefix * 10 + i << ' ';
}
} else if (prefix * 10 + 5 <= up) {
cout << prefix * 10 + 5 << ' ';
}
return;
}
for (unsigned i = 0; i < 10; ++i) {
if (prefix * 10 + i > up) {
return;
} else {
print5_... 阅读全帖 |
|
p********4 发帖数: 58 | 11 我的理解,应该在vector中放地址,而不是object本身。这样才能实现多态。你原来的
做法,你的y中都是 class A. 不知道我的理解对不对。
int main()
{
const A a(1);
const B b(3);
const A *x[2] = { &a, &b };
typedef std::vector V;
V y({ &a, &b });
std::cout << x[0]->f() << x[1]->f() << std::endl;
for (auto i : y)
{
std::cout << i->f();
}
std::cout << std::endl;
return 0;
}
output:
14
14 |
|
R******9 发帖数: 267 | 12 first print the left boundary, then all the leaves, and the right boundary.
1
2 3
4 5 6
7 8 9
should be 1 2 4 7 8 9 6 3.
void traverse(Node* root, bool left_most, bool right_most)
{
if (!root) return; // bottom condition
if (left_most) cout << root->val << ' '; //先序打印左边界
traverse(root->left, left_most, false);
if (!root->left && !root->right && !left_most && !right_most) cout <<
root->val << ' '; //中叙打印叶子
tr... 阅读全帖 |
|
f**********t 发帖数: 1001 | 13 void shuffle(vector &vi) {
for (size_t i = 0; i < vi.size(); ++i) {
size_t k = rand() % (vi.size() - i);
swap(vi[i], vi[i + k]);
}
for_each(vi.begin(), vi.end(), [](int x) {
cout << x << ' ';
});
cout << endl;
}
/*
* Example:
* WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList("the",
"quick", "brown", "fox", "quick"));
* assert(finder.distance("fox","the") == 3);
* assert(finder.distance("quick", "fox") == 1);
*/
class WordDistanceFinder {
vector... 阅读全帖 |
|
m*********a 发帖数: 3299 | 14 第三题
brute force 解法N R*ln(R) R meeting room number
#include
#include
using namespace std;
struct Interval{
int start;
int end;
};
bool comp(Interval a, Interval b){
return a.start
}
class Solution{
public:
int meetingSchedule(vector interval){
sort(interval.begin(),interval.end(),comp);
vector rooms;
if (!interval.empty()){
rooms.push_back(interval[0].end);
}
for (int i=1;i阅读全帖 |
|
m*********a 发帖数: 3299 | 15 第三题
brute force 解法N R*ln(R) R meeting room number
#include
#include
using namespace std;
struct Interval{
int start;
int end;
};
bool comp(Interval a, Interval b){
return a.start
}
class Solution{
public:
int meetingSchedule(vector interval){
sort(interval.begin(),interval.end(),comp);
vector rooms;
if (!interval.empty()){
rooms.push_back(interval[0].end);
}
for (int i=1;i阅读全帖 |
|
k******4 发帖数: 94 | 16 试着写了下time serises那道题,
void printConsectiveLength(vector A)
{
if (A.size() == 0)
return;
stack st;
for(int i=0; i
{
while(st.size() > 0 && A[i] > A[st.top()])
st.pop();
if (st.size() > 0)
cout<
else
cout<
st.push(i);
}
cout<
return;
}
每个index进出stack最多一次,时间O(n),空间O(n). |
|
A*********c 发帖数: 430 | 17 int main() {
const int N = 3;
vector> res(1);
for (int i = 0; i < N; ++i) { //pos
vector> tmpRes;
for (int j = 1; j <= N; ++j) { //value
for (int k = 0; k < res.size(); ++k) { //curr content
vector vecCopy = res[k];
vecCopy.push_back(j);
tmpRes.empla... 阅读全帖 |
|
w*******2 发帖数: 35 | 18 以下是完整的代码
#include
using namespace std;
int main();
{
int X,Y,Z,S,E,F,G;
float m,n,w;
E=5*X;
F=10*Y;
G=20*Z;
S=E+F+G;
m=E/S;
n=F/S;
w=G/S;
cin>>X>>"n";
cin>>Y>>"n";
cin>>Z>>"n";
cout<
cout<
cout<
return(0);
}
用codeblocks运行,结果报错:
error:expected unqualified-id
也不知道是哪里错了,求大家指点,不剩感激~ |
|
T*****n 发帖数: 82 | 19 这个就没bug了
int main() {
// insert code here...
int X,Y,Z,S,E,F,G;
float m,n,w;
cin>>X;
cin>>Y;
cin>>Z;
E=5*X;
F=10*Y;
G=20*Z;
S=E+F+G;
m=E/S;
n=F/S;
w=G/S;
cout<
cout<
cout<
return(0);
} |
|
i******l 发帖数: 270 | 20 我想到了一个办法,刚刚试了一下,确实是O(N)的。
思路就是从两头往中间凑,先得到全部的和,如果满足条件就返回了,
如果不满足,比较两头的大小,每次减去小的那个,然后走一步,代码如下:
int maxSubLen( vector& data, int bar ) {
int sz = data.size(), sum = 0;
for( int i=0; i
if( sum >= bar ) return sz;
int i = 0, j = sz-1;
while( i < j ) {
if( data[i] < data[j] ) sum -= data[i++];
else sum -= data[j--];
if( sum >= bar ) return j - i + 1;
}
return 0... 阅读全帖 |
|
L*******t 发帖数: 782 | 21 以前学C语言时是说数组的名字等价于指向第一个元素的指针。也就是说 a == &a[0]
我刚实验了一下,a == &a == &a[0]。三个都一样。
#include
using namespace std;
int main() {
int a[10];
cout << "a = " << a << endl;
cout << "&a = " << &a << endl;
cout << "&a[0] = " << &a[0] << endl;
}
输出如下:
a = 0xffffcbe0
&a = 0xffffcbe0
&a[0] = 0xffffcbe0
short |
|
r****z 发帖数: 12020 | 22 哈哈
cout<<"我发的我的帖给删了";
for (int i=0;i<100;++i) cout<<"的贴也给删了";
cout< |
|
p******t 发帖数: 10 | 23 include
void main()
{
int age;
if (age<23)
cout<<"life is like a cup of wine, it taste sweet."<
else if(age>23&& age<40)
cout<<"The wine taste a bit sweet, acid,bitter and flavour"<
else
cout<
}
|
|
z****e 发帖数: 2024 | 24 怎么delete?
class A{
public:
A(int i):i(i){
cout<<"A::ctor"<
}
int get() const{
cout<
}
~A(){
cout<<"A::~A()"<
}
private:
int i;
};
template
T* new_with_ctor(int N){
T *arr = static_cast(operator new[] (sizeof(T)*N));
for (int i = 0; i < N; ++i)
new (arr+i) T(i);
return arr;
}
int main(int argc, char* argv[]){
int N=3;
A* p=new_with_ctor(N);
for(int i=0;i
p[i].get();
}
//destruction
for(int i=0;i
|
|
a**U 发帖数: 115 | 25 #include
#include
using namespace std;
class A{
public:
A(){}
~A(){throw "aa";}
};
int main(int argc, char** argv){
try{
A a;
try{
A b;
A c;
}catch(...){
cout<<"kkk1"<
}
}catch(...){
cout<<"kkk2"<
}
cout<<"kkk3"<
return 0;
}
program crash. 请问怎么就crash了?
如果去掉"A c;",一切正常. 谢谢! |
|
P*****f 发帖数: 2272 | 26 http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.3
#include
#include
using namespace std;
class A{
public:
A(){}
~A(){throw "aa";}
};
int main(int argc, char** argv){
try{
A a;
try{
A b;
A c;
}catch(...){
cout<<"kkk1"<
}
}catch(...){
cout<<"kkk2"<
}
cout<<"kkk3"<
return 0;
}
program crash. 请问怎么就crash了?
如果去掉"A c;",一切正常. 谢谢! |
|
a*******h 发帖数: 123 | 27 看看int * a在new之前和之后的值有没有变化?
#include
using std::cout;
using std::endl;
int main(void)
{
int * a;
cout << "a = "<< a << endl;
a = new int[0];
cout << "a = "<< a << endl;
delete []a;
return 0;
} |
|
h**o 发帖数: 347 | 28 1. 现在有AB两个类,由于AB的constructor比较大,又只有f()不相同
我想在初始化B的时候实现动态绑定,请问怎么样可以实现?不成功的代码如下
#include
using namespace std;
class A{
public:
A() { f(); }
virtual void f() { cout << "As class A" << endl; }
};
class B: public A{
public:
B():A() { }
virtual void f() { cout << "As class B" << endl; }
};
int main()
{
B b;
return 0;
}
输出结果为As class A
2. 这个我不太清楚是不是compiler的问题还是什么特别规则,感觉非常奇怪
#include
using namespace std;
class A{
public:
virtual void f() { cout << "As class A" << end |
|
F*****n 发帖数: 1552 | 29 来自主题: Programming版 - C++疑问 关于private里面定义virtual function的两个问题。对于以下程序,
1. a->foo()编译不能通过,说是访问了private member.这是不是表明private里面定
义virtual function毫无意义啊?
2. 既然不被允许访问private member, 这说明complier还是把*a当作B的Object. 然而
a->foo2()也不能通过,因为A没有这个function。为什么作为B的Object连自己public
的member都不能访问呢?
#include
using namespace std;
class A {
int a;
virtual void foo(){
cout << "A:foo" << endl;
}
};
class B:public A{
int b;
void foo(){
cout << "B:foo" << endl;
}
public:
void foo2(){
cout << |
|
y**c 发帖数: 6307 | 30 int a(0);
cout<<(a+=1)<<" "<<(a+=2)<
我得到的结果是
3 2
按照书上的说法,cout<
(cout.operator<<(a)).operator<<(b)
所以我也比较迷惑了。 |
|
x*********h 发帖数: 25 | 31 Hi, I have a question about operator delete, dtor.
define a class like below,
class TestDelete{
public:
virtual ~TestDelete(){
std::cout << "TestDelete::~TestDelete" << std::endl;
}
void operator delete (void* p){
std::cout << "TestDelete::operator delete()" << std::endl;
}
// void operator delete[] (void* p){
// std::cout << "TestDelete::operator delete()" << std::endl;
// }
};
then, write statements like :
TestDelete* p = NULL;
delete p;
I found |
|
c*********n 发帖数: 128 | 32 Does this function have memory problem?
void pushSomthingIntoVector(vector & v) { //T is a class
v.assign(0, T(100)) //T(int a) is a constructor of class T
v.push_back(T(50))
}
void anotherFunction() {
vector v;
pushSomethingIntoVector(v);
cout << v[0] << endl;
cout << v[1] << endl;
}
T(100) and T(50) are created locally within the function
pushSomthingIntoVector(vector & v), does it exsit outside of the function
? say, in the two lines of "cout" ?
Should I change the code as follows: |
|
n**d 发帖数: 9764 | 33 Why the output is the same after I change longjmp(kansas, 47) to longjmp(
kansas, 0)? The code should not go to "else" becasue the return value is 0
for longjmp(kansas, 0).
#include
#include
using namespace std;
class Rainbow {
public:
Rainbow() { cout << "Rainbow()" << endl; }
~Rainbow() { cout << "~Rainbow()" << endl; }
};
jmp_buf kansas;
void oz() {
Rainbow rb;
for(int i = 0; i < 3; i++)
cout << "there's no place like home" << endl;
longjmp(kansas, 47);
}
int main |
|
d*********1 发帖数: 25 | 34 I found a solution on this:
we can define a template to eliminate this problem:
template
class compare
{
T value;
public:
compare(T &v):value(v){}
bool operator(T &s) {
// if ( s
return s
}
}; |
|
n**d 发帖数: 9764 | 35 Shape* sarray[] = {new Circle, new Circle, new Circle};
error C2143: syntax error : missing ';' before '}'
error C2059: syntax error : ';'
The following is the full code.
#include
class Shape {
public:
virtual void draw() = 0;
virtual void erase() = 0;
virtual ~Shape() {}
};
class Circle : public Shape {
public:
Circle() {}
~Circle() { std::cout << "Circle::~Circle\n"; }
void draw() { std::cout << "Circle::draw\n";}
void erase() { std::cout << "Circle::eras |
|
mw 发帖数: 525 | 36 为什么member的constustrctor会比class constructor先调用,奇怪啊
下面code的输出居然是a c b,不解,望赐教
#include
#include
using namespace std;
class a{
public:
a(){cout<<"a"<
};
class c{
public:
c(){cout<<"c"<
};
class b: public a{
public:
b(){cout<<"b"<
c instc;
};
int main(int argc, char *argv[])
{
system("PAUSE");
b instb;
return EXIT_SUCCESS;
} |
|
i***h 发帖数: 12655 | 37 下面的程序我期待 c=129
为什么高位会是ff?
#include
using namespace std;
int
main()
{
cout << (unsigned short)(2>>1 | 1<<7) << endl;
char c = 2>>1 | 1<<7;
cout << "c = " << (unsigned short)c << endl;
cout << "c = " << hex << (unsigned short)c << endl;
}
>>> output:
129
c = 65409
c = ff81 |
|
b***y 发帖数: 2799 | 38 ☆─────────────────────────────────────☆
yapple (Fedora) 于 (Tue Jul 12 17:47:25 2005) 提到:
code is:
#include
using namespace std;
class B
{
public:
B()
{
cout << "construct a new B" << endl;
}
~B()
{
cout << "destruct a B object" << endl;
}
void print()
{
cout << "Hello, I am B!" << endl;
}
};
class A
{
public:
void func()
{
static B* pb;
if(!pb)
{
pb = new B();
} |
|
m*******o 发帖数: 264 | 39 Virtual methods are used for polymorphism. Consider the following three C++
classes:
class A {
public:
void print() { cout << "A"; }
}
class B : A {
public:
void print() { cout << "B"; }
}
class C : B {
public:
void print() { cout << "C"; }
}
Because print is declared as nonvirtual, the method that is invoked depends
on
the type used at compile time:
A *a = new A();
B *b = new B();
C *c = new C();
a->print(); // "A"
b->print(); // "B"
c->print(); // "C"
((B *)c)->print(); // "B |
|
b*****d 发帖数: 23 | 40 The codes following yields results:
ctor
non-const
const
Which I think it is wrong.
It should be
ctor
non-const
non-const
const
////////////////
using namespace std;
class A
{
public:
A(const A&){ cout << "const" << endl;};
A(A&){cout << "non-const" << endl;};
A(){cout << "ctor" << endl;};
static const long i = 1;
static double k;
};
double A::k;
int _tmain(int argc, _TCHAR* argv[])
{
A a = A();
A& b = a;
const A& d = b;
A c(b);
A e(d);
return 0;
} |
|
c*****t 发帖数: 1879 | 41 #include
using namespace std;
class A
{
private:
int m_value;
public:
A (int v) : m_value (v) { std::cout << "ctor" << std::endl; }
~A () { std::cout << "dtor" << std::endl; }
int getValue () { return m_value; }
};
#define SIZE 10
int main ()
{
// init
A* array = (A*)new char[sizeof (A) * SIZE];
for (int i = 0; i < SIZE; ++i)
new (&array[i]) A (5);
for (int i = 0; i < SIZE; ++i)
std::cout < |
|
r*********l 发帖数: 117 | 42 一个实验copy-constructor的程序:
#include
using namespace std;
#define PR(VA) cout<<#VA" = "<
class X{
int i,j,k;
public:
X(int ii=0,int jj=0,int kk=0):i(ii),j(jj),k(kk){}
X(const X& xx){cout<<"copy constructor"<
X& operator=(const X& xx){cout<<"operator ="<
.k*2;return *this;}
void set(int ii,int jj,int kk){i=ii;j=jj;k=kk;}
void print()const{PR(i);PR(j);PR(k);}
};
void func1(X x){x.print();}
X func2(){
|
|
c**a 发帖数: 316 | 43 想不通这个问题, 请指教一下。
如果要访问protected data当然 只能用 private inheritance 不能composition
这个好理解。
但是书上还这么说
if derived class wanna redefine virtual function,
we should we private inheritance.
我觉得不是啊.要redefine virtual function, 咋用都可以。
class Base
{
public:
virtual void f(){std::cout << "Base"; };
};
class DerivedPrivate:private Base
{
public:
virtual void f(){std::cout << "Derived via inheritance"; Base::f();};
};
class DerivedComposite
{
public:
virtual void f(){std::cout << "composite"; b.f();};
private:
Base b |
|
b***y 发帖数: 2799 | 44 ☆─────────────────────────────────────☆
wenchang (随缘@菩提树下,拈花一笑@求道...) 于 (Sun Sep 25 19:33:16 2005) 提到:
how can we make the output of
#include
main(){
cout<<"hello world"<
}
as the following:
initialize
hello world
clean up
without changing the main program.
☆─────────────────────────────────────☆
acrof (KoKo) 于 (Sun Sep 25 19:58:06 2005) 提到:
#include
class A
{
public:
A()
{
cout<<"initialize"<
}
~A()
{
cout<<"clean up"<
}
} |
|
e****d 发帖数: 333 | 45 试试这个,有点笨,但是in place。
不知道能不能满足要求。
VC下编译通过。
#include
#include
using std::cout;
using std::endl;
using std::swap;
int main(){
enum {N=35};
char a[N]=" ab cd efg h i jk lmn op qr";
for(int k=0;k
cout<
}
cout<
int flag=0;
int i=0;
int j=0;
while(j
while(j
if(a[j]!=' '){
flag=0;
swap(a[i],a[j]);
i++;
j++;
}
|
|
s********h 发帖数: 286 | 46 一个很简单的 stream input 小程序,目的是读入名字,ID号码,及其它信息。
我的问题是,如果先读入名字,再读入 ID,就一切正常,可是如果先读入 ID,在读名
字的时候,就会自动跳过,不让我输入名字了。我的程序如下:
#include
using namespace std;
int main()
{
unsigned idNumber;
char firstName[30];
int hoursWorked;
char ch = '\0';
int i = 0;
cout << "Please enter your first name: ";
while (1) {
cin.get(ch);
if (ch == '\n') break;
firstName[i++] = ch;
}
firstName[i] = '\0';
cout << "Please enter your ID Number: ";
cin >> idNumber;
cout |
|
r**e 发帖数: 339 | 47 #include
using namespace std;
int main()
{
//part 1 below
int sum=0,v1;
while(cin>>v1)
{
sum+=v1;
}
cout<< "sum is "<< sum<
//part 2 below
cout<< "imput v2 "<
int v2=0;
cin>>v2;
cout<
return 0;
}
第一部分读入多个数,然可求和.用字母或 ctrl+z 做结束符时,第二部分没有执行读
入v2,直接输出v2结束,返回零.为啥啊? |
|
j*****a 发帖数: 436 | 48 #include
using namespace std;
int main()
{
//part 1 below
int sum=0,v1;
while(cin>>v1)
{
sum+=v1;
cin.get(); //get the space or the return character
}
cin.clear(); // clear out the non-numeric character
cin.get(); // get the return character
cout<< "sum is "<< sum<
//part 2 below
cout<< "imput v2 "<
int v2=0;
cin>>v2;
cout<
return 0;
} |
|
a*****n 发帖数: 149 | 49 刚写了小代码,发现一点疑惑
template
int compare(T &a1, T &a2)
{
if (a1 < a2)
return 1;
else
return 2;
}
void main()
{
cout << compare("abc", "def") << endl;
if ("abc" < "def")
cout << 1 << endl;
else
cout << 2 << endl;
}
第一个输出是2,第二个输出1。为啥呢?多谢赐教! |
|
d*******n 发帖数: 524 | 50 程序在后面。
我的理解是“Hello World”是内存里一段const char[],
foo2之所以不行是因为除了foo2这个function之后,这个char 的array就被release了。
但foo1难道不是相同的情况吗?为什么
cout << foo1() <
这一行就可以输出Hello World呢?
请指教
#include
#include
using namespace std;
char* foo1();
const string& foo2();
int main(int argc, _TCHAR* argv[])
{
int i;
cout << foo1() <
cout << foo2() << endl;
cin >> i;
return 0;
}
char* foo1()
{
return "Hello World";
}
const string& foo2()
{
return "Hello World";
} |
|