都是关于C/C++的问题1:一个函数 void func(char* str),只有一个参数str,如何判断str是以\0结尾的?问题2:一个算法问题,有两个字符串,str1,str2,删除两个字符串中相同的字符,例如str1="abcde",str2="tbbhhy",算法执行后,str1="acde",str2="thhy",这个算法如何实现?问题3:在一个头文件中定义了一个宏,例如:#define MAX(A,B) ((A) > (B) : (A) ? (B)),在另外一个头文件中叶定义了一个同名的宏MAX。在一个C文件中同时include这两个头文件,当我在一个函数中调用MAX宏时,我怎么知道调用的是哪个宏?问题4:单例模式一般这样写:
class A
{
private:
A();
static A a;
public:
static A GetInstance();
};函数GetInstance实现时为:
A A::GetInstance()
{
if (a == NULL)
a = new A(); return a;
}
问题是:如果在多线程环境下,可能两个线程同时调用GetInstance,如何防止a只被创建一次,我知道要加锁,可怎么加锁才能保证高效。大概这些吧,直到正确答案的兄弟帮帮忙,给解决一下,多谢了!
class A
{
private:
A();
static A a;
public:
static A GetInstance();
};函数GetInstance实现时为:
A A::GetInstance()
{
if (a == NULL)
a = new A(); return a;
}
问题是:如果在多线程环境下,可能两个线程同时调用GetInstance,如何防止a只被创建一次,我知道要加锁,可怎么加锁才能保证高效。大概这些吧,直到正确答案的兄弟帮帮忙,给解决一下,多谢了!
2.2级上机题目;如果字符不长的话
3.你自己再调用一次, 看看结果是和哪个宏的结果一样 就是哪个
4.我也不太清楚,增加一个flag 貌似就可以解决。
char str1[] = "abcde";
char str2[] = "tbhchy";
cout << str1 << endl;
cout << str2 << endl;
for(int i = 0; str1[i] != 0 ;++i)
{
for(int j = 0; str2[j] != 0 ;++j)
{
if(str1[i] == str2[j])
{
int m = i;
for( ;str1[m] != 0 ;++m)
{
str1[m] = str1[m + 1];
}
int n = j;
for(;str2[n] != 0 ;++n)
{
str2[n] = str2[n + 1];
}
}
}
}
cout << str1 << endl;
cout << str2 << endl;
{
private:
A();
public:
static A& GetInstance();
}; 函数GetInstance实现时为:
A& A::GetInstance()
{
static A a;
return a;
}
char str2[] = "taebddddddgggaeaoaaeeey";
int count = 0;
cout << "清除前:" << endl;
cout << str1 << endl;
cout << str2 << endl; int i = 0;
int j = 0;
int flag = 0; while(str1[i])
{
char c_t = str1[i];
while(str2[j])
{
if(str2[j] == c_t)
{
//清理B串并重置j的值。
for(int m = j; str2[m] != 0; ++m)
{
str2[m] = str2[m + 1];
}
flag = 1;
}
else
{
j++;
}
}
j = 0;
if(flag)
{
//A串也要清理并重置i的值。
///*
for(int n = i; str1[n] != 0; ++n)
{
if(str1[n] == c_t)
{
for(int q = n ;str1[q]; ++q)
{
str1[q] = str1[q + 1];
}
n--;
}
}
//*/
/*
int n = i;
do
{
if(str1[n] != c_t)
{
n++;
}
else
{
for(int q = n ;str1[q]; ++q)
{
str1[q] = str1[q + 1];
}
n--;
}
}while(str1[n]);
*/
flag = 0;
}
else
{
i++;
}
}
cout << "清除后:" << endl;
cout << str1 << endl;
cout << str2 << endl;