我正在尝试使用此代码从文件中读取并将值存储在向量中.这适用于一次并正确显示所有内容.
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) 为什么以下代码不编译?
#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)
除了参考语言规则外,请详细说明这些规则背后的基本原理.
为什么构造函数和虚拟析构函数的存在会因初始化而停止?
我只是想确认这是一个很好的方法,只允许我只在文件夹上工作,因为我可以看到一种只查找文件夹而不是文件和文件夹的方法.
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) 我想更改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) 我在以下程序中遇到 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++ 进行计算的应用程序,然后使用 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和C++中?
说res=(1 & 2 & 3 & 4),这是res1=(1&2)和res2=(3&4)和
res= (res1 & res2).这会是一样的吗?
我正在学习重载内联函数.
在测试这段代码时,我无法使用以下错误进行编译,我无法理解错误.
我只用一个内联功能测试它,然后工作但第二次打破它.你能否请一些指导:
谢谢你的帮助,塞尔吉奥
编译器错误:
#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) std::stack我们用apop()来提取最后一个成员并top()获取它的值。是否有任何快捷方式可以同时执行这两个操作(获取最后一个成员的值并将其踢出)?
下面的代码编译得很好(除非调用method没有注释)。
.
extern "C"
{
struct S
{
int some_int;
void method(){}
};
}
int main()
{
S s();
// s.method();
return 0;
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
arrays ×1
boost ×1
c ×1
c++11 ×1
c++17 ×1
constructor ×1
extern ×1
inline ×1
memory ×1
overloading ×1
pybind11 ×1
stack ×1
std ×1
struct ×1
templates ×1
type-traits ×1
visual-c++ ×1