我对 gtest/gmock 进行了很多研究,但没有一个给我正确的答案。我是 C++ 新手,所以任何帮助都将不胜感激。
我正在努力创建 Docker Compose 来创建 Redis 集群。我看到 Bitnami 有一个 Redis 集群映像,我尝试了,但由于以下错误,我的 Spring Boot 应用程序无法连接到它:

我尝试了另一种方法是创建 2 个主从 Redis 实例,我可以连接到它。现在我尝试创建 6 个 Redis 实例,然后使用以下命令创建一个包含 3 个主服务器和 3 个从服务器的 Redis 集群:
redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 \
127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 --cluster-replicas 1
Run Code Online (Sandbox Code Playgroud)
但是当我执行命令时它说
Could not connect to Redis at 127.0.0.1:6380: Connection refused
Run Code Online (Sandbox Code Playgroud)
以下是我当前的 Docker-compose.yaml:
version: '3.8'
services:
redis-node-0:
image: redis:latest
container_name: redis-0
ports:
- "6380:6379"
command: ["redis-server","--appendonly yes","--cluster-enabled yes","--cluster-node-timeout 5000"]
volumes:
- redis-cluster_data-0:/redis/data
redis-node-1:
image: redis:latest
container_name: redis-1
ports:
- "6381:6379"
command: …Run Code Online (Sandbox Code Playgroud) 我只能使用 C++ 标准库 (C++14) 将时间戳转换为给定格式的日期时间。我刚接触 C++,我知道 C++ 并没有通过像 Java 这样的库为我们提供很多支持。在中欧时区 (CET)的给定日期和时间2011-03-10 11:23:56,将产生以下标准格式输出:“ 2011-03-10T11:23:56.123+0100 ” .
std::string format = "yyyy-MM-dd'T'HH:mm:ss'.'SSSZ"; //default format
auto duration = std::chrono::system_clock::now().time_since_epoch();
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
Run Code Online (Sandbox Code Playgroud)
我的格式字符串的语法是
G : Era
yy,yyyy : year (two digits/four digits)
M,MM : month in number (without zero/ with zero) - (e.g.,1/01)
MMM,MMMM : month in text (shortname/fullname)- (e.g.,jan/january)
d,dd : day in month (without zero/with zero)- (e.g.,1/01)
D : day in year
F : day of week …Run Code Online (Sandbox Code Playgroud) 问题是我不知道输入字符串的长度。如果输入字符串为“ yyyy”,则只能替换我的函数。我认为解决方案是,首先,我们将尝试将输入字符串转换回“ yyyy”,并使用我的函数完成工作。
这是我的功能:
void findAndReplaceAll(std::string & data, std::string toSearch, std::string replaceStr)
{
// Get the first occurrence
size_t pos = data.find(toSearch);
// Repeat till end is reached
while( pos != std::string::npos)
{
// Replace this occurrence of Sub String
data.replace(pos, toSearch.size(), replaceStr);
// Get the next occurrence from the current position
pos = data.find(toSearch, pos + replaceStr.size());
}
}
Run Code Online (Sandbox Code Playgroud)
我的主要功能
std::string format = "yyyyyyyyyydddd";
findAndReplaceAll(format, "yyyy", "%Y");
findAndReplaceAll(format, "dd", "%d");
Run Code Online (Sandbox Code Playgroud)
我的预期输出应为:
%Y%d
Run Code Online (Sandbox Code Playgroud) 我以为NaNJava和C#中只有一个值,但是我发现NaNC ++中有一些不同类型的值。它们具有相同的含义吗?它们之间有什么区别:
cout << numeric_limits<double>::infinity() - numeric_limits<double>::infinity() << endl; // -nan(ind)
cout << numeric_limits<double>::quiet_NaN() << endl;//nan
if ((numeric_limits<double>::infinity() - numeric_limits<double>::infinity()) == numeric_limits<double>::quiet_NaN()) {
cout << "THE SAME" << endl;
}
else {
cout << "NOT THE SAME"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
它可以打印出来NOT THE SAME,但是当我std::isnan()用来测试值是否NaN均为true时。
isnan(numeric_limits<double>::infinity() - numeric_limits<double>::infinity());// true
isnan(numeric_limits<double>::quiet_NaN()); // true
Run Code Online (Sandbox Code Playgroud) c++ ×3
c++14 ×3
c++-chrono ×1
googlemock ×1
googletest ×1
spring-boot ×1
str-replace ×1
timestamp ×1