小编Fra*_*eux的帖子

C++为什么我重新启动程序时文件会被截断?

我正在尝试使用此代码从文件中读取并将值存储在向量中.这适用于一次并正确显示所有内容.

void SongList::LoadSongsFromFile()
{
    song temp;
    string line;
    ifstream myFile("SongListFile.txt");
    while (getline(myFile, line)) {
        myFile >> temp.title;
        myFile >> temp.artist;
        myFile >> temp.genre;
        songs.push_back(temp);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我想使用以下内容附加到该文件:

void SongList::AddSong(song tmp)
{
    cout << "Enter the title, artist then genre of the song, each on a new line.\n";
    cin >> tmp.title;
    cin >> tmp.artist;
    cin >> tmp.genre;
    songs.push_back(tmp);
    ofstream myFile("SongListFile.txt");
    myFile.open("SongListFile.txt", ios::app);
    myFile << tmp.title << " " << tmp.artist << " " << tmp.genre;
    cout << tmp.title << " by …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++

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

为什么带有支撑初始化列表的Constructor/virtual析构函数不起作用?

为什么以下代码不编译?

#include <vector>

class Foo
{
public:
    Foo()
    { }

    virtual ~Foo()
    { }

    std::vector<int> aVec;
};


Foo bar =
{
    { 1, 2, 3, 4, 5 }
};
Run Code Online (Sandbox Code Playgroud)

以下代码编译时:

#include <vector>

class Foo
{
public:

    /*Foo()
    { }

    virtual*/ ~Foo()
    { }

    std::vector<int> aVec;
};


Foo bar =
{
    { 1, 2, 3, 4, 5 }
};
Run Code Online (Sandbox Code Playgroud)

除了参考语言规则外,请详细说明这些规则背后的基本原理.

为什么构造函数和虚拟析构函数的存在会因初始化而停止?

c++ constructor virtual-destructor

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

c ++ Boost if .extension()==""这意味着这个文件是一个文件夹?

我只是想确认这是一个很好的方法,只允许我只在文件夹上工作,因为我可以看到一种只查找文件夹而不是文件和文件夹的方法.

boost::filesystem::directory_iterator iterator(string("."));
for (; iterator != boost::filesystem::directory_iterator(); ++iterator)
{
    if ((iterator->path().extension()) == "") { 
        cout << (iterator->path().stem()) <<   endl; 
    };
}
Run Code Online (Sandbox Code Playgroud)

c++ boost

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

为什么这个程序没有打印返回迭代器的正确的第二个元素?

我想更改map元素的值,如果它已经存在于map容器中,即计算该容器中元素的no

std :: map < int, int > m;
std :: map < int, int > :: iterator itr;

int arr[] = { 10, 40, 20, 20, 20, 20, 20, 20, 10, 30, 10, 30, 40 };

for (int i : arr) {
    itr = m.find(i);
    if (itr == m.end() ) {
        int value = 0;
        m.insert(std :: make_pair(i, value));
    } else {
        ++itr->second;
    }
}
itr = m.begin();
while (itr != m.end() ) {
    std :: cout << itr->first …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

std::is_invocable 的奇怪行为

我在以下程序中遇到 std::is_invocable 问题:

#include <iostream>
#include <type_traits>

void g() {}

template<bool B, typename T>
void f(T t) {
    if constexpr (B)
        g(t);
}

int main() {
    std::cerr << std::boolalpha <<
        std::is_invocable_v<decltype(&f<true, int>), int> << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我原以为程序输出为假,因为 f<true, int> 无法实例化。但是,GCC(从 10.0.1 20200224 开始)不会编译,错误消息为

test.cpp: In instantiation of 'void f(T) [with bool B = true; T = int]':
test.cpp:14:51:   required from here
test.cpp:9:10: error: too many arguments to function 'void g()'
    9 |         g(t);
      |         ~^~~
test.cpp:4:6: note: declared …
Run Code Online (Sandbox Code Playgroud)

c++ templates type-traits language-lawyer c++17

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

可以将连续放置在内存中的变量视为数组的一部分吗?

我正在编写一个使用 C++ 进行计算的应用程序,然后使用 pybind11 将多维结果作为 numpy 数组返回。从pybind的文档和在线看到的示例来看,numpy 数组创建基本上是传递数据数组的指针并包含步幅的详细信息。然而,在 C++ 部分,我并不热衷于使用一维array和使用一些花哨的索引,但我宁愿使用结构。这让我思考是否可以将放置在连续内存中的(同质)变量视为array.

我的思路如下。an 的元素array被放置在连续内存中。a 的元素struct也按其声明的顺序连续放置(当不涉及填充时)。因此,从内存放置的角度来看,以下四个变量声明是相同的,例如,如果我要指向第一个元素的指针,那么我可以通过一次执行一个整数值的步骤来遍历所有元素:

struct struct_array
{
    int elem[4] = {};
};

struct struct_ints
{
    int a = {};
    int b = {};
    int c = {};
    int d = {};
};

// integer matrix of shape 3x4
int one_dim_array[3 * 4] = {};
int two_dim_array[3][4] = {};
struct_array array_of_struct_arrays[3] = {};
struct_ints array_of_struct_ints[3] = {};
Run Code Online (Sandbox Code Playgroud)

这是我的测试代码,表明我的问题的答案是肯定的。它执行一些地址打印、设置和读取元素。

#include <iostream> …
Run Code Online (Sandbox Code Playgroud)

c++ memory arrays pybind11

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

是C和C++中的按位AND运算符传递?

我有一个问题:是否按位传递,特别是在C和C++中?

res=(1 & 2 & 3 & 4),这是res1=(1&2)res2=(3&4)res= (res1 & res2).这会是一样的吗?

c bitwise-operators

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

内联过载抛出错误c ++

我正在学习重载内联函数.

在测试这段代码时,我无法使用以下错误进行编译,我无法理解错误.

我只用一个内联功能测试它,然后工作但第二次打破它.你能否请一些指导:

谢谢你的帮助,塞尔吉奥

编译器错误:

  • abs异常规范与先前的声明行13不匹配
  • function long abs(const long)throw()已经有了body line 13
  • abs重新定义; 不同的异常规范行19
  • 函数定义或声明中的abs错误; 功能不称为第30行
  • 函数定义或声明中的abs错误; 函数不称为第32行

#include "pch.h"
#include <iostream>
using namespace std;

// Overload abs() three ways
inline int abs(int n)
{
    cout << "In integer abs() \n";
    return((n < 0) ? -n : n);
}

inline long abs(long n)
{
    cout << "In long abs() \n";
    return((n < 0) ? -n : n);
}


inline double abs(double n) {
    cout << "In double abs() \n";
    return …
Run Code Online (Sandbox Code Playgroud)

c++ overloading inline visual-studio

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

堆栈结合了 pop() 和 top() 吗?

std::stack我们用apop()来提取最后一个成员并top()获取它的值。是否有任何快捷方式可以同时执行这两个操作(获取最后一个成员的值并将其踢出)?

c++ stack std

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

在 c 结构上调用默认构造函数

下面的代码编译得很好(除非调用method没有注释)。

  1. 为什么允许“调用”默认构造函数?(应该没有)
  2. 为什么成员函数的声明不是错误?

.

extern "C"
{
    struct S
    {
        int some_int;
        void method(){}
    };
}

int main()
{
    S s();
//    s.method();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ struct extern

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