小编Chr*_*isD的帖子

将字符串推回到对象的向量

我正在浏览我为学校项目编写的一些代码,经过仔细检查,我觉得很奇怪.我有一个类似下面的类:

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

c++ type-conversion implicit-conversion

8
推荐指数
2
解决办法
814
查看次数

在多地图中搜索价值

假设我有以下内容:

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.

c++ multimap c++11

2
推荐指数
1
解决办法
1637
查看次数

为什么我的if语句不起作用

我的代码是

#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条件.

c numbers

-3
推荐指数
1
解决办法
80
查看次数