由买买提看人间百态

topics

全部话题 - 话题: endles
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
o*******p
发帖数: 722
1
in C++:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef vector> wcs;
bool myfakeless(pair a, pair b)
{
return (a.second>b.second);
}
wcs findkfwords(const char* fname, int k)
{
wcs results;
ifstream fs(fname);
if (k<1)
{
cerr << "bad k" < return resu... 阅读全帖
f****4
发帖数: 1359
2
来自主题: JobHunting版 - 问个面试题
void printspiral(char *a, int colsize, int m, int n, int x0, int y0)
{
// a is the matrix, colsize is its original column size
// m is #rows, n is #cols in the submatrix
// x0 and y0 are the offsets of the first element of the submatrix
// check if m and n are positive
if (m<=0 || n<=0) {
cout << "zero or negative dimensions" << endl;
return;
}
int i;
if(m>=2&&n>=2) {
// print the outer circle
for(i=0; i<=n-2; i++)
cout << a[x0*c... 阅读全帖
w*******r
发帖数: 2953
3
来自主题: JobHunting版 - 一道OO设计题
程序和运行结果如下,不考虑瘸子的情况。
#include
using namespace std;
class human
{
public:
human();
human(bool, bool);
~human(){};
bool getEyeState();
bool getMouthState();
char *getHealthDesc();
private:
bool isBlind;
bool isDeaf;
};
human::human()
{
isBlind = 0;
isDeaf = 0;
}
human::human(bool blind, bool deaf)
{
isBlind = blind;
isDeaf = deaf;
}
bool human::getEyeState()
{
return isBlind;
}
bool human::getMouthState()
{
return isDeaf;
}
char *hu... 阅读全帖
y*****z
发帖数: 9
4
来自主题: JobHunting版 - A facebook interview question
我用了 基序排序, 这样能达到线性时间。
复杂度是 max(O(M),O(N))
M是date的最大的数
N是数组的长度
code wins arguments.
欢迎拍砖
#define GREEDY_EVENT
#ifdef GREEDY_EVENT
#include
#include
#include
using namespace std;
int main(){
const int n = 5;
int sstart[n] = {1,8,2,11,15};
int eend[n] = {4,9,6,14,18};
for(int i=0;i cout<<"("< }
cout< const int MAXDATE = 100;
int radix[MAXDATE]={0};
int final[n];
for(int ... 阅读全帖
y*****z
发帖数: 9
5
来自主题: JobHunting版 - A facebook interview question
我用了 基序排序, 这样能达到线性时间。
复杂度是 max(O(M),O(N))
M是date的最大的数
N是数组的长度
code wins arguments.
欢迎拍砖
#define GREEDY_EVENT
#ifdef GREEDY_EVENT
#include
#include
#include
#include
using namespace std;
int main(){
srand(size_t(time(0)));
const int n = 5;
int sstart[n] = {1,8,2,11,15};
int eend[n] = {4,9,6,14,18};
for(int i=0;i for(int i=0;i for(int i=0;i cout<<"("<阅读全帖
i**********e
发帖数: 1145
6
来自主题: JobHunting版 - 报个offer@FG,回报版面
我刚在linux上运行了,没有这方面的问题,不过我的程序是C++ 不是Java。
我测试了你的算法,返回结果都算满意,而且运行越多次,返回的结果越接近理想的 p
值。
你的算法不单简洁,而且复杂度是最优的,递归是 average 两次就返回答案。
expected # calls = 1*0.5 + 2*0.5^2 + 3*0.5^3 + ...
= Summation( k * 0.5^k ), k = 1 .. n
= 2 - (n+2)/2^n
When n = infinity,
expected # calls = 2
贴下code方便大家做测试:
#include
#include
#include
#include
using namespace std;
bool rand2() {
return (rand() % 2 == 0);
}
unsigned long long totalCalls = 0;
bool Prob2(double p, bool expected = tru... 阅读全帖
i**********e
发帖数: 1145
7
来自主题: JobHunting版 - 报个offer@FG,回报版面
我刚在linux上运行了,没有这方面的问题,不过我的程序是C++ 不是Java。
我测试了你的算法,返回结果都算满意,而且运行越多次,返回的结果越接近理想的 p
值。
你的算法不单简洁,而且复杂度是最优的,递归是 average 两次就返回答案。
expected # calls = 1*0.5 + 2*0.5^2 + 3*0.5^3 + ...
= Summation( k * 0.5^k ), k = 1 .. n
= 2 - (n+2)/2^n
When n = infinity,
expected # calls = 2
贴下code方便大家做测试:
#include
#include
#include
#include
using namespace std;
bool rand2() {
return (rand() % 2 == 0);
}
unsigned long long totalCalls = 0;
bool Prob2(double p, bool expected = tru... 阅读全帖
a**U
发帖数: 115
8
来自主题: JobHunting版 - 一个C++的问题!
来源于面试题目,下面的code可以编译运行成功。关键是我觉得不应该编译运行成功,
请看下面我的comments。请高手指点。
#include
using namespace std;
class A
{
public:
virtual void test(){ cout <<"test A"< };
class B : public A
{
public:
virtual void test(){ cout <<"test B"< };
class C : public A
{
int value;
public:
C(){value = 1; }
virtual void test(){ cout <<"test C"< void accessValue() { cout << "value=" << value << endl; }
void setValue(int value){ this->value... 阅读全帖
B*******1
发帖数: 2454
9
来自主题: JobHunting版 - 问一个老数组题
似乎可以吧,刚测试了一下,没有发现不对啊。
测了这几个例子
72 cout << "-----------------Test 1--------------------" << endl;
73 int test1[] = {1, -2, 4, -2, 6, 7};
74 getSubArray(test1, sizeof(test1)/sizeof(int), 7);
75
76 cout << "-----------------Test 2--------------------" << endl;
77 int test2[] = {1,1,1,1,-1};
78 getSubArray(test2, sizeof(test2)/sizeof(int), 3);
79
80
81 cout << "-----------------Test 3--------------------" << endl;
82 int test3[] = {1,1,1,1,0,-1,0,-1,1};
83 getSubAr... 阅读全帖
g*******n
发帖数: 214
10
来自主题: JobHunting版 - 弱问一道c++语法题
是那道打印括号的题目:addValidParenthesis是一个另一个函数,返回的结果是正确
的,因为我不用指针的话(如下面代码)是可以答应出来的。
vector Parenthesis::generateParenthesis(int n){
vector list;
addValidParenthesis(n-1, n, &list, "(");
/*cout< for(int i=0;i cout< }
cout< return list;
}
但是如果改为vector* 返回类型,并且上面代码只修改为return &list。在外
面用
vector* result = Parenthesis::generateParenthesis(3);
用 cout << (*result)[0] << endl;是打印不出来东西
请... 阅读全帖
b********s
发帖数: 1676
11
来自主题: JobHunting版 - 请教一个查找算法问题
没看懂,还是多谢code了。测试了一下,貌似second max不对。。。不管k换成3还是4
,都给第一个72,第二个是69,第三个是70.
int main()
{
int a[10]={39,31,34,53,24,70,42,69,72,44};
int *p;
int size = 0;
p = findPath(a,0,10,size);
cout<<"max="< cout<<"second max="< cout<<"p[3]="< cout<<"p[4]="< getchar();
return 1;
}
r***e
发帖数: 29
12
来自主题: JobHunting版 - BBC 编程风格面试题,求指教
最后个人标准答案
#ifndef _ROMON_HEADER_
#define _ROMON_HEADER_
#define _DEBUG_
#include
#include
//ROMON digits
const std::string ROMON_DIGITS[] = {"I","IV","V","IX", "X","XL","L","XC","C"
,"CD","D","CM","M" };
//ROMON scale
const int ROMON_SCALE[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
1000};
const int ROMON_MAX = 3999;
const int ROMON_MIN = 1;
class RomanNumeralGenerator
{
public:
virtual std::string generator(int num) = 0;
};
class CRoman : public RomanNumeralGene... 阅读全帖
s********u
发帖数: 1109
13
来自主题: JobHunting版 - 问一个关于stringstream的诡异问题
今天在做波兰表达式的时候想到用一下stringstream。
试了一下,发现>>与<<的行为非常诡异,代码和输出如下:
string str = "abc xyz";

stringstream stream(str);

cout << stream.str() <
stream >> str;

cout << str < stream << str;

cout << stream.str() <
stream >> str;

cout << str < 就是将一个字符串吐出来,再塞回去,这时候stream里的字符串是一样的。
但是再吐出来,就不是这个abc字符串了,而是他后面那个,也就是说abc是塞到流的最
后了。但是stream.str()却一致。太诡异了。
p****o
发帖数: 46
14
来自主题: JobHunting版 - 问一个关于stringstream的诡异问题
you can check output position for insert (tellp) and input position for
extraction (tellg).
string str = "abc xyz";

stringstream stream(str);
// tellp() returns 0; tellg returns 0;

cout << stream.str() <
stream >> str;

// extract: input moves from 0 to 3 (tellg() ==3), which is the
whitespace position
cout << str < stream << str;
// insert: since no insertion so far, output position is still 0,
so it ... 阅读全帖
f**********t
发帖数: 1001
15
来自主题: JobHunting版 - f家店面题
int FlogCrossRiver(string river) {
if (river.empty()) {
return 0;
}
vector>> vp(river.size());
vp[0].emplace_back(1, 1);
int res = INT_MAX;
for (size_t i = 0; i < vp.size(); ++i) {
if (river[i] == '0') {
continue;
}
for (auto pr : vp[i]) {
if (i + pr.first >= vp.size()) {
res = min(pr.second, res);
} else if (river[i + pr.first] == '1') {
vp[i + pr.first].emplace_back(pr.first, pr.second + 1);
}
if... 阅读全帖
f**********t
发帖数: 1001
16
来自主题: JobHunting版 - f家店面题
int FlogCrossRiver(string river) {
if (river.empty()) {
return 0;
}
vector>> vp(river.size());
vp[0].emplace_back(1, 1);
int res = INT_MAX;
for (size_t i = 0; i < vp.size(); ++i) {
if (river[i] == '0') {
continue;
}
for (auto pr : vp[i]) {
if (i + pr.first >= vp.size()) {
res = min(pr.second, res);
} else if (river[i + pr.first] == '1') {
vp[i + pr.first].emplace_back(pr.first, pr.second + 1);
}
if... 阅读全帖
s*w
发帖数: 729
17
来自主题: JobHunting版 - [leetcode] merge k lists 求助
不知道为啥,我的 heap 时灵时不灵的?请看下面的 code 和输出显示
#include
#include
#include
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int v):val(v),next(NULL) {}
};
bool minHeapPredicate(ListNode* lhs,ListNode* rhs) {
return lhs->val > rhs->val;
}
class Solution {
public:
ListNode *mergeKLists(vector lists) {
ListNode *retHead = NULL, *retTail = NULL;
// store values and nodes into heap
vector ... 阅读全帖
l****s
发帖数: 75
18
来自主题: JobHunting版 - A question about C++. Thanks.
nothing happens.
a and b are not initialized.
try this:
#include
class foo
{
private:
int a;
int *b;
public:
void print()
{
std::cout << a << std::endl;
std::cout << &a << std::endl;
std::cout << b << std::endl;
//std::cout << *b << std::endl;
}
};
void doSth()
{
foo f;
f.print();
}
void main()
{
doSth();
std::cin.get();
}
c**********e
发帖数: 58
19
来自主题: JobHunting版 - A question about C++. Thanks.
a is not initialized to 0, nor b is initialized to NULL. Try this
class foo
{
public:
// foo() {cout << a << " " << b << endl;};
int a;
int *b;
};
void doSth()
{
foo f;
cout << f.a << " " << f.b << endl;
}
int main() {
doSth();
// int a;
// cout << a << endl;
// int *b;
// cout << b << endl;
return 0;
}
w****a
发帖数: 710
20
来自主题: JobHunting版 - 急求大神指导一道面经
C++11的
condition_variable gOReady, gHReady;
int atomcountH = 2, atomcountO = 1;
void O() {
unique_lock l(gLock);
gHReady.wait(l, []{return atomcountO > 0;});

cout << "O" << endl;
if(--atomcountO == 0 && atomcountH == 0) {
atomcountH = 2;
atomcountO = 1;
cout << "water" << endl;
}
gOReady.notify_all();
}
void H() {
unique_lock l(gLock);
gOReady.wait(l, []{return atomcountH > 0;});

cout << "H" << endl;
if(--atomcou... 阅读全帖
t**r
发帖数: 3428
21
来自主题: JobHunting版 - topcoder- strange country problem.
贴个答案
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
const long long LINF = (5e18);
const int INF = (1<<30);
#define EPS 1e-6
const int MOD = 1000000007;
using namespace std;
class StrangeC... 阅读全帖
s*********y
发帖数: 615
22
来自主题: JobHunting版 - 发两个软件组的面试题
在会议室里看到上个礼拜面试新毕业生的东西,,发两个出来,估计可能对大家也没啥
实际作用,反正等着开会也没啥事干,,就当给自己以后找工作攒个人品了。。呵呵,
,我不会软件组的,所以也不知道难易,也没答案
behavioral:
What is the hardest software problem that you have debugged. how did you go
about doing it? what caused the bug
XML Parser(我们的东西会用到xml。所以)
XML exists to define tests available on some system:




Design and implement a class to:
1.store and manage... 阅读全帖
b*****l
发帖数: 9499
23
来自主题: Thoughts版 - OpenMP 求救。。。
在学 OpenMP,第一步就不通:设多线程失败。。。
TestOMP.cpp 的 code 很简单:开 5 个线程,每个介绍一下自己,就完事了.
#include
#include
using namespace std;
main () {
omp_set_num_threads(5);
cout << "Fork! " << endl;
#pragma omp parallel
{
// Obtain and print thread id
cout<< "Hello World from thread = " << omp_get_thread_num()
<< " of " << omp_get_num_threads() << endl;
// Only master thread does this
if (omp_get_thread_num() == 0)
cout << "Master thread: number of threads = " <<
omp... 阅读全帖
l********a
发帖数: 1154
24
又来一次
#include
#include
#include
using namespace std;
int main()
{
ifstream fin("test.txt");
string curWord, lastWord = "";
fin >> curWord;
while (curWord.compare("end")!=0)
{
if (lastWord.empty())
{
cout << curWord << " COMES FIRST" << endl;
}
else if (curWord>lastWord)
{
cout << curWord << " AFTER " << lastWord << endl;
}
else if (curWord {
... 阅读全帖
b*****l
发帖数: 9499
25
来自主题: Linux版 - OpenMP 求救。。。 (转载)
【 以下文字转载自 Thoughts 讨论区 】
发信人: bigsail (河马·旋木), 信区: Thoughts
标 题: OpenMP 求救。。。
发信站: BBS 未名空间站 (Sat Apr 30 02:18:47 2011, 美东)
在学 OpenMP,第一步就不通:设多线程失败。。。
TestOMP.cpp 的 code 很简单:开 5 个线程,每个介绍一下自己,就完事了.
#include
#include
using namespace std;
main () {
omp_set_num_threads(5);
cout << "Fork! " << endl;
#pragma omp parallel
{
// Obtain and print thread id
cout<< "Hello World from thread = " << omp_get_thread_num()
<< " of " << omp_get_num_threads() << endl;
... 阅读全帖
j*******a
发帖数: 101
26
来自主题: Programming版 - reverse words, not the Microsoft one!!!
i wrote one. it works well.
#include
using namespace std;
char* reverse_word (char* str);
int main(int argc, char** argv){
char str[] = "this is very beautiful ";
cout <<"["< cout << "["<< reverse_word(str)<<"]"< char str2[] = "jokeslala rejected some 100k offers ";
cout <<"["< cout << "["<< reverse_word(str2)<<"]"<
system("pause");
return 0;
}
char* reverse_word (char* str){
if (str =
d*******n
发帖数: 524
27
来自主题: Programming版 - 数组定义的时候,分配空间了么?
//test.cpp
#include
using namespace std;
int main() {
int a[5] = {11, 22, 33, 44, 55};
int b[5];
int c[5];
int d[5] = {1, 2, 3, 4, 5};
cout << "&a =\t" << (long)&a << endl
<< "&b =\t" << (long)&b << endl
<< "&c =\t" << (long)&c << endl
<< "&d =\t" << (long)&d << endl;
}
$ ./a.out
&a = 140733237530704
&b = 140733237530672
&c = 140733237530640
&d = 140733237530608
m**a
发帖数: 46
28
来自主题: Programming版 - 数组定义的时候,分配空间了么?
using namespace std;
int main() {
int a[5] = {11, 22, 33, 44, 55};
int b[5];
int c[5];
int d[5] = {1, 2, 3, 4, 5};
cout << "&a =\t" << (long)&a << endl
<< "&b =\t" << (long)&b << endl
<< "&c =\t" << (long)&c << endl
<< "&d =\t" << (long)&d << endl;
}
$ ./a.out
&a = 140733237530704
&b = 140733237530672
&c = 140733237530640
&d = 140733237530608
is this correct? a,b,c,d are pointers already, what will we get from &a, &b,
&c, and &d? maybe you should use (long)&a[0] instead of (long)&a.
m******t
发帖数: 4077
29
来自主题: Programming版 - 问个简单的memory allocation 的问题。
1, 以下小程序里没有给b allocate space, 为什么编译器不报错呢?g++
2, a[5]到底在内存里面占几个字节?是5个吗?但是最后一个已经给了 '\0', 对吗?
3, a[5]是在stack中还是在heap中啊?如果用new 给b分配地址的话是在heap中,对吧?
#include
using namespace std;
int main()
{
char * b;
char a[5] = "1234";
strcpy(b, a);
cout << a < cout << b << endl;
cout << "size of b " << sizeof(b)< cout << "size of a " << sizeof(a)< return 0;
}
d*******d
发帖数: 2050
30
来自主题: Programming版 - 来,出个题
非常非常简单。
先自己写下结果,然后运行试试。
void f(char a[]){
cout << sizeof(a) << endl;
cout << sizeof(*a) << endl;
}
main(){
char a[10];
cout << sizeof(a) << endl;
cout << sizeof(*a) << endl;
f(a);
}
s***e
发帖数: 122
31
来自主题: Programming版 - 关于c++的constructor的面试题
现在明白了,呵呵,怎么说来着,it's more than clear now.
主要是我们如果用顺序的方式去解释,就没法反映其中的层次。而把compiler生成的整个函数体写出来之后,就清晰多了。
刚才很有兴趣的用Java也测试了一下,发现结果是一样的。不过把virtual函数放到constructor之后就有不同的效果了。
C++输出是:
a
a.f()
c
b
Java输出是:
a
b.f()
c
b
// =========== C++ Implementation ===========
#include
#include
using namespace std;
class a{
public:
a(){cout<<"a"< virtual f() {cout<<"a.f()"< };
class c{
public:
c(){cout<<"c"< };
class b: public a {
public:
b(){cout<<"b"< virtual f() {c
m*******o
发帖数: 264
32
来自主题: Programming版 - 编译错误问题
#include
#include
#include
using namespace std;
class Phones{
public:
Phones(){cout << "Phone object created!" << endl;} //Create and and
initialize phone object
virtual void isDialing() { cout << "base virtual " << endl;}
virtual ~Phones(){ cout << "~Phone" << endl; } //Destroy the Phone
object
protected:
string PhoneNumber;
string ModelType;
};
class LandlinePhone: public Phones{
public:
LandlinePhone(){cout << "LandlinePhone" << endl;}
voi
w********p
发帖数: 948
33
#include
using namespace std;
class Base
{
public:
Base(){ cout<<"Constructor: Base"< ~Base(){ cout<<"Destructor : Base"< };
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived"< ~Derived(){ cout<<"Destructor : Derived"< };
int main()
{
Derived *Var = new Derived();

Base Var2;
Var2 = (Base) *Var;
delete Var;
return 0;
}
output:
Constructor: Base //1
Co
z****e
发帖数: 2024
34
来自主题: Programming版 - 问一个C++文件读取的问题
int i, k;
//Create a file
ofstream OUTPUT( "RESULT.dat"); //ios::out | ios::in );
if( !OUTPUT ){
cerr << "File could not be opened." << endl;
exit( 1 );
}
//Input the data
for( i=1; i<=8; i++ ){
k = i*10;
cout << " k = " << k << endl;
OUTPUT.write( reinterpret_cast( &k ), sizeof(int) );
}
OUTPUT.close();
cout << endl << "Start Output" << endl;
ifstream INPUT("RESULT.dat");
INPUT.seekg( 2*sizeo
z****e
发帖数: 2024
35
来自主题: Programming版 - 再问C++初始化问题。
为了验证我的一些想法,我写了如下代码,
标号2,4的运行区别不是很理解。
class X{
public:
int i;
};
X* px=new X;
X gox;
int main(){
X* lpx=new X;
std::cout<i< std::cout<i< X ox;
std::cout< std::cout< }
结果:
0
0
0
-1077381160
主要是2,和4,为什么2和4有区别呢?
stack和heap上,初始化还不一样?
stack和静态区不一样是可以理解的。
另外
X* lpx=new X;

X* lpx=new X();
貌似没有区别,实际有没有区别呢?
因为heap上要zero out,所以heap的效率要轻微的低于stack???
p*u
发帖数: 2454
36
来自主题: Programming版 - c++ 设计问题求助

anywhere by
setter/
you are right, but we can always solve this with another level of
indirection:
"
#define GETTER( a ) Intruder::get( *this, a )
#define SETTER( a, i ) Intruder::set( *this, a, i )
class A
{
private:
friend struct Intruder;
int _i;
};
struct Intruder
{
template
static int get( const T& ref, const A& a );
template
static void set( const T& ref, A& a, int i );
};
template
int Intruder::get( const T& ref, const A& a )... 阅读全帖
s*******o
发帖数: 392
37
本人仍在继续学习cplusplus。com网站的教程,
看到了static member这一章节,
这是例子code
#include
using namespace std;
class CDummy
{
public:
static int n;
CDummy(){ n++ ;};
~CDummy(){ n--; };
};
int CDummy::n = 0;
int main()
{
CDummy a;
CDummy b[5];
CDummy*c = new CDummy;
cout << b[2].n < cout << c->n < delete c;
cout << CDummy::n < cout << b->n <
system("PAUSE");
return 0;
}
我的问题是问什么在delete c之前n输出值为7, 而在delete c之后的输出值时6?大家
能不能
试一试?而且... 阅读全帖
a****m
发帖数: 693
38
#include
#include
#include
using namespace std;
// this function removes trailing spaces from x
void trim (char *str, int size )
{
int i;

/* remove the null terminator */
for ( i = size; i >0; i=i-1 )
{
if ( str[i] == '\n' || str[i]=='\t' || str[i]==' ' )
{
str[i-1] = '\0';
/* we're done, so just exit the function by returning */
return;
}
}
/* if we get all the way to her... 阅读全帖
f******5
发帖数: 11
39
来自主题: Programming版 - Please help me take a look at the program
Thanks so much.
The compiler I am using is Dev c++ 4.9.9.2
Can I use New operator inside LCS like follows? It shows more compiling time
error?
Or should I declare c[][] outside the helping function LCS or inside Main()
function?
I want to pass the arrayc[][] and b[][] at the end of LCS to another Helping
function called
Print_LCS.
From your point of view, where should I declare the c[][] and b[][]
If i use New operator as follows to declare and initialize c[][], c[][],
both c[][] and b[][] go ou... 阅读全帖
a***y
发帖数: 2803
40
#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++;
(a[3]=4)=5;
cout << "a[0] is "<< a[0] << endl;
cout << "a[1] is "<< a[1] << endl;
cout << "a[2] is "<< a[2] << endl;
cout << "a[3] is "<< a[3] << endl;
return 0;
}
结果是
a[0] is 9
a[1] is 1
a[2] is 2
a[3] is 5
(a[3]=4)=5;最后a[3]等于5,不是4. 语法正确,结果正确.

say
operators:
precedenc
kick
ta
x******a
发帖数: 6336
41
来自主题: Programming版 - 请教for_each
为什么下面这个程序输出的时候多一个1出来:
enter the number of element
5
4 0 3 0 4 1
the maximum element is 4
0 0 3 4 4
程序在这
#include
#include
#include
using namespace std;
void myfunction(int i)
{
cout << i<<" ";
}
int main()
{
int N;
cout<< "enter the number of element" < cin>> N;
int a[N];
srand((unsigned) time(0));
for(int i=0; i a[i]=rand()%N;
cout< cout<< endl;
cout<<"the maximum element is " << *max_element(a, a+N)... 阅读全帖
f***t
发帖数: 25
42
来自主题: Programming版 - 一个诡异的const_cast问题
对于简单类型的const_cast,下面代码:
int const i=2; //现在i值const了
int* k = const_cast(&i); //用引用也行,结果是一样的
*k=3; //借被const cast 的k改i值;成功;
cout< cout<<*k< cout<<&i< cout< 以上是VC6的编译。
把简单类型换成类就不会有这种现象,这是不是VC6的一个bug?
r**a
发帖数: 536
43
来自主题: Programming版 - 菜鸟请教smart pointer
下面的code中,输出为"Constructor, Constructor, Destructor, 3" 前两个
constructors分别是create b1 and b2是出现的。第3个Destructor应该是对应到“b1
= b2;"。我的问题是此时b2已经被free掉了,为啥最后的结果是3呢?哪位大牛给解释
解释。谢谢了。
#include
#include
#include
#include
class StrBlob
{
public:
typedef std::vector::size_type size_type;
StrBlob();
StrBlob(std::vector &il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_... 阅读全帖
s*w
发帖数: 729
44
改了两个小时,终于改好了一半
现在一个 producer 一个 consumer 没问题了;上个版本的 consumer 没循环,只能处
理一行;而且在睡觉的时候没有  guard spurious wakeup, 我以为用 if可以算
guard 了,其实需要 while loop来guard 或者用 data2consume.wait(lck,!data.
empty())
糟糕的是 多 consumer 还是不行,会 deadlock 。
再次呼唤高人指点一下
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXCAPACITY = 3;
mutex mx;
deque data;
map freq;
c... 阅读全帖
t**r
发帖数: 3428
45
try this,
you will understand
1 using namespace std;
2 #include
3 #include
4
5 int main(){
6 int *a,*b;
7 cout<<&a<<" "<<&b< 8 cout< 9 a=b;
10 cout<<&a<<" "<<&b< 11 cout< 12 return 0;
13 }
m*********a
发帖数: 3299
46
来自主题: Programming版 - c++ copy reference 还是deep copy
vector v;// a v vector
{
Entity e;//init an entity object on stack and set health to 100
Entity *ptr=&e; //point ptr to e
v.push_back(ptr);//copy the reference(?) ptr to vector v
cout<<"In the block"< cout<getHealth()< }//e is destroyed and e out of range
cout<<"Out of the block"< cout<getHealth()< // print out 100, is the object point... 阅读全帖
D***n
发帖数: 6804
47
你可以看看这个打出来什么:
const char i='x';
const char *cp=&i;
char *p;
p=const_cast(cp);
*p='y';
std::cout<<*p< std::cout<<*cp< std::cout< const char *t = &i;
std::cout<<*t< 我这里的结果是 y y x y
C*****n
发帖数: 1049
48
来自主题: Programming版 - 出道面试题
这道题我在snapchat onsite的时候也遇到了,以前没见过,当时只给出了dfs的解。
现在给出dfs和dp的解,可能有bug,没做深度测试。
#include
#include
using namespace std;
//basically we can use a dfs to solve the problem
bool jumpDfs(vector & stone_pos, int pos, int speed) {
int n=stone_pos.size();
if(pos==n-1) return true;
if(!stone_pos[pos] || pos>=n || speed<1) return false;
for(int s=speed-1; s<=speed+1; ++s) {
if(jumpDfs(stone_pos,pos+s,s)) return true;
}
return false;
}
//assume init_spee... 阅读全帖
c***s
发帖数: 15
49
来自主题: Programming版 - 请教一个C++的设计问题
//样本代码如下,把具体decoder方法在不同type上实现就行
#include
enum decoder_type
{
type_A = 0,
type_B = 1,
type_C = 2,
no_decoder_available = 3
};
template
class decoder_algorithm
{
};
template<>
struct decoder_algorithm
{
static bool is_compatible () { return false; }
static void decode() { std::cout << "decoder A" << std::endl; }
};
template<>
struct decoder_algorithm
{
static bool is_compatible () { return false; }
static void decode() { std::cout << "decoder B" ... 阅读全帖
y**b
发帖数: 10166
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)