我在 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) 我目前正在为我的计算机科学模块编写一个项目,但是如果 double 值中有太多小数位,我的字符串值会默认为科学记数法的问题。
我试着显而易见的解决方案ostringstream,并.str()却使得它变成符号。我必须编译到 C++98 标准,所以我不能使用像std::to_string.
我需要将值转换为字符串,但它需要保持其格式。任何帮助,将不胜感激。
我正在尝试将一个简单的 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) 运行以下代码:
methodOne(){
std::thread t(&ChatBubble::runChatBubbleThread, this);
t.join();
}
Run Code Online (Sandbox Code Playgroud)
runChatBubbleThread:
runChatBubbleThread(){
// code here
sleep(2000);
// more code here
}
Run Code Online (Sandbox Code Playgroud)
我的理解是新威胁t被创建,执行其代码,然后在主线程完成后加入主线程,有没有理由sleep()在主线程中休眠?
我唯一能想到的就是t.join在主线程继续之前等待线程完成,但是如果它必须等待则最后是线程的重点!
工作案例:
template<typename T>
class threadsafe_queue
{
private:
mutable std::mutex mut;
std::queue<T> data_queue;
public:
threadsafe_queue()
{}
threadsafe_queue(const threadsafe_queue& other)
{
std::lock_guard<std::mutex> lk(other.mut);
data_queue=other.data_queue;
}
};
Run Code Online (Sandbox Code Playgroud)
案例应该会失败:注意不mutable上std::mutex mut;
template<typename T>
class threadsafe_queue
{
private:
std::mutex mut;
std::queue<T> data_queue;
public:
threadsafe_queue()
{}
threadsafe_queue(const threadsafe_queue& other)
{
std::lock_guard<std::mutex> lk(other.mut);
data_queue=other.data_queue;
}
};
Run Code Online (Sandbox Code Playgroud)
我已经尝试过上面列出的两种情况,并且编译没有问题.我假设内部lock_guard调用mutex :: lock函数,它本身不是const函数.
问题>为什么我们可以从复制构造函数中的const对象锁定互斥锁?
下面给出的是一个简单的程序,它将输入输入到矢量并尝试使用迭代器打印矢量.为什么不打印矢量?一切似乎都没问题.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
int n,m,i;
cin >> n >> m;
vector<int> v;
v.reserve(m);
vector<int>::iterator it;
for(i=0;i<m;i++)
cin >> v[i];
// for(i=0;i<m;i++)
// cout << v[i];
for(it = v.begin();it != v.end();it++)
cout << *(it);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果输入"4 3 1 2 3",为什么程序没有输出?
我正在为我的考试而学习,我遇到了这个例子,它有一行说:
for(;i<=m;i++)
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么;前面没有任何东西?它有什么作用?这是什么意思?
我正在尝试为我的C ++程序创建一个日志文件。我的目标是在程序的两点放置两个时间戳,并在文件中打印这两点之间的CPU时间周期。我这样做是因为我想知道代码的哪些部分最耗时,因此可以进行改进(因此可能要测量几块代码)。到目前为止,我已经制作了一个函数,该函数在被调用时将作为参数传递的字符串打印到文件中:
#define LOGFILE "myprog.log"
void Log (std::string message){
std::ofstream ofs;
ofs.open(LOGFILE, std::ofstream::out | std::ios::app);
ofs << message << std::endl;
ofs.close();
}
Run Code Online (Sandbox Code Playgroud)
但是,我很难弄清楚如何打印CPU时间戳。首先,我不知道应该使用哪种时间测量格式(我应该使用chrono还是time_t类型?)我正在尝试打印时间段,因此,如果存在持续时间类型(我会我尝试了chrono :: duration,但似乎需要C ++ 11支持)。其次,鉴于我知道要使用哪种类型,如何将其打印到文件中?有没有一种方法可以将该类型转换为字符串?还是可以将其直接传递给函数并以某种方式打印?
最近几天这让我很烦恼,我似乎无法弄清楚,因此任何输入都会很有帮助。提前致谢!
所以我们的讲师只是向我们展示了如何连接两个链接列表然后继续并向我们展示了如果实现了将会出错的示例,我似乎并不完全理解为什么它不能正常工作,因为它不是解释.如果我以这种方式连接两个链表,会出现什么问题:
template <typename T>
void LList<T>::concat(LList<T> const & list) {
end->next = list.start;
end = list.end;
}
Run Code Online (Sandbox Code Playgroud) 我被教导要做:
functionReturn function(dataType_of_parameter)
Run Code Online (Sandbox Code Playgroud)
虽然我越来越多地看到了
functionReturn function(dataType_of_parameter parameter_identifier)
Run Code Online (Sandbox Code Playgroud)