cin >> *integerVar >> *charVar;可以正确读取"25 b"之类的输入.使用现有字符串执行此操作的最简单方法是什么(我可以通过拆分然后解析每个部分来手动完成,但更好的方法是什么)?
有没有办法扩展/修改/实现constexpr来返回多维C风格数组维度的std :: tuple?
#include <iostream>
#include <tuple>
template<typename T, std::size_t N>
auto arraysize(T (&arr)[N])
{
return std::make_tuple(N);
}
Run Code Online (Sandbox Code Playgroud)
IOW,如何使arrayize适用于任何维数组?以上"工作"但只返回一个维度:
int i[5];
std::cout << std::get<0>(arraysize(i)) << std::endl;
Run Code Online (Sandbox Code Playgroud)
工作,并返回5.并且,
char c[3][4];
std::cout << std::get<0>(arraysize(c)) << std::endl;
Run Code Online (Sandbox Code Playgroud)
工作,并返回3,但是
std::cout << std::get<1>(arraysize(c)) << std::endl;
Run Code Online (Sandbox Code Playgroud)
不编译因为arraysize()没有正确编码.有没有办法将其编码为constexpr来处理任何维数组?尝试使用参数包但没有成功.
以下C++代码是否正确?
struct Base { int x; };
struct Derived : Base { int y; }
Base * b = new Base;
Derived * d = static_cast<Derived *>(b);
//below we access only d->x, but not d->y
std::cout << d->x;
Run Code Online (Sandbox Code Playgroud)
如果没有,究竟出了什么问题?C++标准对此有何看法?至少我没见过它曾经坠毁过.
我正在编写一个大富翁游戏.而且我真的很喜欢用于制作漂亮干净输出的排序功能,在这种情况下,按照组对玩家拥有的属性进行排序.
首先,这是我的属性分类器功能
bool propertySort(Property a, Property b) { return a.getGroup() < b.getGroup(); }
Run Code Online (Sandbox Code Playgroud)
现在,玩家拥有这些属性的列表.
#pragma once
#include <string>
#include <vector>
#include "Dice.h"
#include "Property.h"
class Player
{
public:
...
private:
int curMoney{ 1500 };
int curLocation{ 0 };
int playerNum{ 0 };
std::string name;
std::vector<Property> ownedProperties{};
int curDiceRoll;
};
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,它是一个底层的私有属性,它被归为0属性.
因此,在我的主要物业分拣机中,我会在棋盘上显示属性以供玩家查看,分拣机功能不断给我一个错误C2280.我知道正是这种排序函数抛出了这个错误,因为如果我注释掉排序行,程序运行正常.我确信这是显而易见的,因为我是初学程序员,所以我一无所知.如果有人能够提供有关什么是伟大的见解,谢谢!
void viewProperties(Player& p)
{
string prompt = "Select a player to view their properties";
vector<string> playerNames{};
for (Player p : players)
{
playerNames.push_back(p.getName());
}
Menu propertiesMenuSelection{ prompt, playerNames }; …Run Code Online (Sandbox Code Playgroud) 当我输入下面的代码时
int x = 1;
+++x;
Run Code Online (Sandbox Code Playgroud)
它会被分成++(+x),当然这个句子是错误的,因为 后面有一个右值++。
我很好奇为什么不能+(++x),其中代码是正确的。
这取决于 IDE 还是编译器?
可以在 C++ 标准中找到吗?或者这只是一个未定义的行为?
非常感谢回答这个问题并原谅我糟糕的英语。
如何将包含 0 和 1 的二进制数组累加为整数?
vector<int> arr = {1,0,1,0,1,1,1,0,1,0,0};
int num = accumulate(
arr.begin(), arr.end(),
[](int &a, int &b)
{
// ???
}
);
Run Code Online (Sandbox Code Playgroud)
在每一步我都需要这样的东西:
if(arr[i] % 2) num += pow(2, i);
Run Code Online (Sandbox Code Playgroud)
但是如何在 lambda 中实现这一点呢?如何进入ilambda?
我在 C++ 应用程序中使用 boost-property-tree,尝试读取 JSON 文件users.json并将数据存储到对象 ( ) 向量中std::vector<User> users;。
JSON 文件如下所示:
{
"OperatingSystem":"Windows 10",
"users" :
[
{
"firstName":"John",
"lastName":"Black"
},
{
"firstName":"Kate",
"lastName":"Red"
},
{
"firstName":"Robin",
"lastName":"White"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我已OperatingSystem使用以下代码行成功读取了该属性:
boost::property_tree::ptree treeRoot;
boost::property_tree::read_json("users.json", treeRoot);
std::string operatingSystem = treeRoot.get<std::string>("OperatingSystem");
std::cout << "OS : " << operatingSystem << std::endl;
Run Code Online (Sandbox Code Playgroud)
效果很好。
为了存储用户,我创建了一个 User 类。您可以看到下面的头文件User.hpp:
#ifndef USER_H
#define USER_H
#include <iostream>
#include <string>
class User
{
private:
// Properties
std::string firstName;
std::string lastName; …Run Code Online (Sandbox Code Playgroud) int number_generator(bool new_id)
{
if (new_id)
{
srand(time(NULL));
int x = 99999 + (rand() % 999999);
return x;
}
else
{
throw std::runtime_error("Oops! something went wrong");
}
}
Run Code Online (Sandbox Code Playgroud)
给定一个真参数,此函数生成一个随机的 6 位数字。我已经编译了很多次,它生成的数字总是以“1”开头。我究竟做错了什么?
我正在尝试将一个简单的 C 程序从 64 位 Ubuntu Linux 交叉编译为 aarch64 (arm64)。有人可以帮我解释一下为什么我会收到此错误吗?
它说找不到“cpuid.h”。我试过在64位linux上编译它,它工作得很好。但是使用的时候aarch64-linux-gnu-gcc却报错。
我收到以下错误。
aarch64-linux-gnu-gcc -O1 -fno-stack-protector -march=armv8-a test.c -o test
test.c:4:10: fatal error: cpuid.h: No such file or directory
4 | #include <cpuid.h>
| ^~~~~~~~~
compilation terminated.
Run Code Online (Sandbox Code Playgroud)
内容test.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cpuid.h>
// Requires that the user input the CPUID,
// plus the bytes "3" and "Q";
void succeed(char* string) {
printf("Yes, %s is correct!\n", string);
exit(0);
}
void fail(char* string) {
printf("No, …Run Code Online (Sandbox Code Playgroud) 这就是我现在正在做的事情:
double fb0(const double *x, const double *par){ // B0'
return -par[0]+(1-Cxr(0,2)*par[1])*x[0]+Cxr(0,2)*par[1]*x[0];
}
double fb1(const double *x, const double *par){ // B1'
return -par[0]+(1-Cxr(1,2)*par[1])*x[1]+Cxr(1,2)*par[1]*x[1];
}
double fb2(const double *x, const double *par){ // B2'
return -par[0]+(1-Cxr(2,2)*par[1])*x[2]+Cxr(3,2)*par[1]*x[3];
}
double fb3(const double *x, const double *par){ // B3'
return -par[0]+(1-Cxr(3,2)*par[1])*x[3]+Cxr(4,2)*par[1]*x[4];
}
double fb4(const double *x, const double *par){ // B4'
return -par[0]+(1-Cxr(4,2)*par[1])*x[4]+Cxr(5,2)*par[1]*x[5];
}
...
Run Code Online (Sandbox Code Playgroud)
一直持续到fb20。然后我调用我的void main(){}. 我想做的是在我的之前有这样的事情void main(){}:
for(int i=0;i<20;i++){
double fb[i](const double *x, const double *par){return -par[0]+(1-Cxr(i,2)*par[1])*x[i]+Cxr(i+1,2)*par[1]*x[i+1];} …Run Code Online (Sandbox Code Playgroud)