vector::erase由于某种原因,当比较语句内部的返回值if或者先存储值然后比较时,我会得到不同的结果。但这似乎只发生在 g++ 中。
这是使用 g++、libstdc++ 和-std=c++14flag 在 Ubuntu 20.04 AMD64 上构建的。
g++ -std=c++14 foo.cpp && ./a.out
这将返回false
#include <vector>
#include <iostream>
#include <algorithm>
int main()
{
    std::vector<int> v{0, 1, 8, 3, 8, 5, 8, 7,8, 9};
    int thing_id{9};
    std::vector<int>::iterator const cit{
        std::remove_if(v.begin(),
                        v.end(),
                        [thing_id](int const& thing) -> bool {
                            return thing == thing_id;
                        })};
    if (v.erase(cit, v.cend()) == v.cend()) {
        std::cout << "true\n";
        return true;
    }
    else {
        std::cout << "false\n";
        return false;
    } …是否有可能没有专门的矢量?
我的问题是:我有一个抽象类N4GestureRecognizer和它的几个子类.所以在一个Controller类中我想要一个vector<N4GestureRecognizer> recognizers_但是因为它是抽象的我不能.如何将这个识别器存储在矢量或集合或列表中,或者在标准c ++中可以循环使用?
为什么这不能在gcc上编译?
#include <iostream>
#include <string>
int main() {
    double f = 23.43;    
    std::wstring f_str = std::to_wstring(f);
    std::wcout << f_str << std::endl;
    return 0;
}
错误:
prog.cpp:在函数'int main()'中:
prog.cpp:6:26:错误:'to_wstring'不是'std'的成员
流式传输stringstreamlibstdc ++扩展?这个程序与编译gcc-4.2,gcc-4.7-2 (using -std=c++03)和铛3.2使用-std=c++11和libstdc++(感谢安迪警车,见注释).它不clang 3.2使用-std=c++11和编译-stdlib=libc++.
#include<iostream>
#include<sstream>
int main() {
  std::stringstream s; s << "b";
  std::cout << "ss: " << s << std::endl;
  return 0;
}
通过查看ofstream的构造函数,它可以采用a std::basic_streambuf<CharT, Traits>*或a basic_ostream& st.字符串流是一个std::basic_istream,但两者都是std::basic_ios<CharT, Traits>如此,我猜它应该工作.
以下更改使代码在clang下编译:
  std::cout << "ss: " << s.str() << std::endl;
做正确的方法是什么?cout << s;还是cout << s.str();?
我对这个事实感到有点惊讶
#include <string>
#include <cassert>
#include <cstdio>
int main()
{
    printf("Floating point format: %.7g\n",1.4);
    std::string a("Hello, World/#");
    std::string b("Hello, World 2");
    assert(a>b); 
    assert(setlocale(LC_ALL,"sv_SE.UTF-8")!=NULL);
    printf("New floating point format: %.7g\n",1.4);
    assert(b<a);
}
http://coliru.stacked-crooked.com/a/1b435636e4ff7161
该程序正常退出。结论,语言环境会影响 a 和 b 之间的比较。那是对的吗?这意味着当使用 std::string 作为 std::set 等中的键时,排序不变量会因当前语言环境的改变而被破坏,而没有自定义比较函数。
考虑以下计划:
#include <cstdio>
#include <cmath>
int main()
{
    int d = (int)(abs(0.6) + 0.5);
    printf("%d", d);
    return 0;
}
g++7.2.0输出0(参见此处的现场演示)
g++6.3.0(参见此处的现场演示)
prog.cc: In function 'int main()':
prog.cc:6:26: error: 'abs' was not declared in this scope
     int d = (int)(abs(0.6) + 0.5);
                          ^
prog.cc:6:26: note: suggested alternative:
In file included from prog.cc:2:0:
/opt/wandbox/gcc-6.3.0/include/c++/6.3.0/cmath:103:5: note:   'std::abs'
     abs(_Tp __x)
     ^~~
clang++5.0.0输出1(在此处查看现场演示)
clang++3.6.0(点击此处观看现场演示)
prog.cc:6:19: error: use of undeclared identifier 'abs'; did you …这是测试我的问题的代码。
#include <iostream>
#include <stack>
using namespace std;
int main(){
    int num;
    int Array[1];
    stack<int> Stack;
    cout << "Int size " << sizeof(num) <<endl; // Output: Int size 4
    cout << "Array size " << sizeof(num) <<endl; // Output: Array size 4
    cout << "Stack size " << sizeof(Stack) <<endl; // Output: Stack size 80
    return 0;
}
我试图了解内存空间分配。通常 int 内存大小为 4 个字节。但是,当我初始化一个Stackint 数据类型时std::stack,Stack大小为 80 字节。
应该是4吗?为什么要std::stack占用 80 个字节?或者堆栈内部实际上是什么大小为 80 字节?
最近我正在查看 std::array 头文件,我发现了一些奇怪的定义,例如 _GLIBCXX20_CONSTEXPR 或 _GLIBCXX17_CONSTEXPR 等。为什么需要它们?
一些背景信息,对于一项家庭作业,我不得不使用二叉树编写一个波兰语表示计算器,为此,我必须解析命令行输入,以便它可以正确地构建二叉树,然后遍历它来给出有效答案。输入的数学表达式。
为了进行解析,我使用了std :: stringstream,以便可以轻松地将std :: string转换为有效的float(或整数,双精度)。我遇到的问题是以下代码,其中显示了错误以及如何解决该问题。我希望有人可以告诉我我做错了什么.clear()不正确,或者这是否是标准库中处理此特定输入的方式中的错误(仅适用于+和-)。
#include <iostream>
#include <sstream>
#include <string>
int main() {
    std::string mystring("+");
    int num;
    char op;
    std::stringstream iss(mystring);
    iss >> num;
    // Seems it is not a number 
    if (iss.fail()) {
            // This part does not work as you would expect it to
            // We clear the error state of the stringstream
            iss.clear();
            std::cout << "iss fail bit: " << iss.fail() << std::endl;
            iss.get(op);
            std::cout << "op is: " << op << " iss is: …我有链接在这里我push(x)10 int秒,然后pop()11和大小不是0,或一个例外,但一个重要的数字(可能== std::numeric_limit<size_type>::max()).我认为这是内部表示只是做一个size--而不是检查已经empty()案例的结果.这似乎是stdc ++库中的一个错误.
我正在通过Ivor Horton的Beginning Visual C++ 2012工作,我在第三章练习二:决策和循环.这是我的代码:
#include <iostream>
using namespace std;
int main()
{
    char letter;
    int vowels = 0;
    do
    {
        cout << "Enter a letter, and enter q or Q to end" << endl;
        cin >> letter; // Enter a letter
        switch (letter)
        {
            case 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U': // If letter is a vowel, add to vowels variable
                vowels++; …我的开发/生产环境都是 CentOS-7.7。为了用 gcc-8.3.0 编译我的程序,我在我的开发环境中安装了“devtoolset-8”,但它不能像 CentOS7 最初附带的 gcc-4.8.5 一样使用。每次需要编译程序时,必须使用“scl enable devtoolset-8 -- bash”切换到gcc8而不是gcc4.8.5。
当程序部署到生产环境时,没有gcc8,也没有libstdc++.so.6.0.25,所以它不能运行。我猜 libstdc++.so.6.0.25 应该和 gcc8 一起发布?我既不能在生产环境上安装“devtoolset-8”,也不能在生产环境上从源代码构建 gcc8。CentOS官方yum repo安装的libstdc++版本是libstdc++.so.6.0.19,所以我的程序无法在production-env中加载。如何让这样的程序运行?
谢谢!请原谅我丑陋的英语。
我正在致力于将 C++ 库包装到 C 桥中。
\n\n所有对象,我\xe2\x80\x99d 喜欢在堆上使用shared_ptrs 进行维护,例如:
\n\nvoid* makeFoo() {\n  return new shared_ptr<void>(shared_ptr::make_shared<Foo>());\n}\n我可以使用这样的通用销毁吗:
\n\nvoid destroy(void* p) {\n  delete static_cast<shared_ptr<void>*> p;\n}\n或者有更干净的方法吗?
\n