我正在浏览我为学校项目编写的一些代码,经过仔细检查,我觉得很奇怪.我有一个类似下面的类:
class Foo {
public:
Foo(std::string s) : _s(s) {}
private:
std::string _s;
};
int main() {
std::string str = "Hiyo";
std::vector<Foo> f;
f.push_back(str); // Compiles. Weird to me though.
f.push_back(Foo(str)); // Predictably, this compiles as well.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么第一次调用push_back有效语句,即使str不是Foo?
假设我有以下内容:
class Foo {
public:
Foo(int x) {
_x = x;
}
int _x;
}
int main() {
multimap<string, Foo> mm;
Foo first_foo(5);
Foo second_foo(10);
mm.insert(pair<string, Foo>("A", first_foo));
mm.insert(pair<string, Foo>("A", second_foo));
Foo third_foo(10);
}
Run Code Online (Sandbox Code Playgroud)
检查third_foowith 密钥"A"是否已经在我的multimap.
我的代码是
#include <stdio.h>
int main()
{
int n;
unsigned long int a,x;
scanf("%d",&n);
while(n--)
{
scanf("%lu",&a);
int count=0;
while(a!=0)
{
x=a%10;
printf("x=%lu\n",x );
if(x!=4||x!=7)
{
count++;
}
printf("count =%d\n",count );
a=a/10;
}
//printf("%d\n",count );
}
}
Run Code Online (Sandbox Code Playgroud)
我观察到当x = 4或x = 7计数增加时,它不应该因为我在其中放入了if条件.