考虑 R 中的可重现示例:
test <- c(1:12)
> test
[1] 1 2 3 4 5 6 7 8 9 10 11 12
Run Code Online (Sandbox Code Playgroud)
预期结果:
test.list <- split(test, gl(2, 3))
> test.list
$`1`
[1] 1 2 3 7 8 9
$`2`
[1] 4 5 6 10 11 12
Run Code Online (Sandbox Code Playgroud)
我正在尝试用 C++ 编写等效的代码来生成并返回由 test.list 产生的两个向量。请注意,我在 C++ 中处于尴尬的新手阶段。
我编写了一个简单的时钟,发现虽然小时和秒都可以,但如果我在一行上声明变量,分钟就不行了(分钟从 16 开始,而不是像预期的那样从 0 开始)。
如果我在单独的行上声明变量,问题就解决了。我还是很好奇,有人知道为什么吗?
这是代码:
#include <stdio.h>
#include <windows.h>
//h=hours, m=minutes, s=seconds.
int main(){
int h, m, s = 0; //THIS IS THE LINE: WHY "m" STARTS AT 16 AND NOT 0?
int delay = 1000;
while(1){
s++;
if(s>59){
m++;
s=0;
}
if(m>59){
h++;
m=0;
}
if(h>12){
h=1;
}
printf("\n %02d:%02d:%02d", h, m, s);
Sleep(delay);
system("cls");
}
}
Run Code Online (Sandbox Code Playgroud) 我想用光谱颜色填充曲线下的区域,得到这样的图。
这就是我尝试过的
ggplot(bq, aes(x=w.length, y=s.e.irrad)) +
geom_segment(aes(xend=w.length, yend=0, colour=abs(w.length)^0.7*sign(w.length))) +
geom_line() +
scale_colour_gradient2(low=scales::muted("blue"),
mid=scales::muted("green"),
high=scales::muted("red"))
Run Code Online (Sandbox Code Playgroud)
得到这个
还尝试使用 geom_area
ggplot(bq, aes(x = w.length, y = s.e.irrad))+
geom_area(fill = "steelblue") #steelblue is for example
Run Code Online (Sandbox Code Playgroud)
但无法填充渐变
我的数据框的 x 为波长,y 为辐照度
我有 5 列包含数字数据,我想过滤与 5 列中至少 3 列中的数据范围匹配的行。
例如,我有以下数据框,我定义值范围为 5-10。我的第一行有 3 列,其值在 5 到 10 之间,所以我想保留该行。第二行只有 5 到 10 之间的 2 个值,所以我想删除它。
| 第1列 | 列2 | 第3栏 | 第4栏 | 第5栏 |
|---|---|---|---|---|
| 7 | 4 | 10 | 9 | 2 |
| 4 | 8 | 2 | 6 | 2 |
这是我的代码:
#include <iostream>
int main()
{
char x = 32;
while (x <= 126) {
std::cout << x << "\n";
x += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
到这里为止,一切顺利,但如果我将代码更改为:
#include <iostream>
int main()
{
char x = 32;
while (x <= 127 /* here the "bad" change */ ) {
std::cout << x << "\n";
x += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
为了尝试打印 [del] 字符,我的程序进入无限循环并开始打印许多我不想要的其他字符。为什么?
我试图在地图中存储一些派生类。
我使用 share_ptr 存储它们以避免意外的释放。
不幸的是,在我的尝试中,它是有效的:程序编译并执行,但我收到一条错误消息。
我获得了以下MWE:
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <typeindex>
#include <typeinfo>
using namespace std;
class DataInOut {
public:
std::shared_ptr<void> data = nullptr;
std::type_index type = typeid(nullptr);
bool initialized = false;
bool optional = false;
// template <class Archive>
virtual bool dummy_funct(int &ar, const char* charName){
cout<< "serialize_or_throw from DataInOut address is an empty function." << endl;
return true;
}
DataInOut *clone() const { return new DataInOut(*this); }
~DataInOut(){}; // Destructor
};
template <typename …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释这段代码有什么问题吗?有时它会起作用,即如果我在终端上输入 5, 5, 5, -1,它会返回 15。但其他时候,它会返回 0。
#include <iostream>
#include <vector>
using namespace std;
int main() {
int input;
vector<int> input_vector;
cout << "Enter -1 when done" << endl;;
cout << "Your int: ";
cin >> input;
while (input != -1) {
input_vector.push_back(input);
cout << "Your int: ";
cin >> input;
}
int sum = 0;
for (auto i : input_vector) {
cout << "i: " << input_vector[i] << endl;
sum += input_vector[i];
}
cout << "Sum: " << sum …Run Code Online (Sandbox Code Playgroud) 我的问题:
intfloatcharstr这些 var 类型都有为其分配的标准化字节分配。但我从来没有了解过这一点的指针:
这些“地址”存储变量,其内容看起来只是一系列字母数字字符。
C++代码:
int *ptr1;
char *ptr2;
void *ptr3;
Run Code Online (Sandbox Code Playgroud)
那么上面列出的指针的标准内存分配是什么呢?一般来说呢?
我试图获取向量的大小作为数字,以便我可以将其用作constexpr。
返回vector.size()的大小类型不是常量表达式。因此,我想用它sizeof(vector) / sizeof(vector[0])来获取一个整数值,我可以在 constexpr 初始化中手动使用它。
const vector<int> int_vec = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << sizeof(int_vec) / sizeof(int_vec[0]) << endl;
Run Code Online (Sandbox Code Playgroud)
我的 CLion 编译器总是打印 6。我不明白为什么。任何帮助将不胜感激。
感谢您的回复。我了解到我可以用来sizeof(arr) / sizeof(arr[0])查找数组的长度。那么这对于向量怎么会不起作用呢?
这是我的代码:
\nempty <- ""\n\nmtcars %>% select(mpg,empty)\nRun Code Online (Sandbox Code Playgroud)\nError in `select()`:\n! Can't subset columns that don't exist.\n\xe2\x9c\x96 Column `` doesn't exist.\nRun Code Online (Sandbox Code Playgroud)\n该empty对象是 for 循环的输出。所以如果空等于""我不需要选择任何内容,但会出现以下错误。
换句话说,我应该能够选择这样的东西:mtcars %>% select(mpg,)。我需要转变""为
我怎样才能避免这个错误?
\nc++ ×6
r ×4
allocation ×1
ascii ×1
c ×1
dplyr ×1
fill ×1
filter ×1
ggplot2 ×1
pointers ×1
rcpp ×1
shared-ptr ×1
split ×1
tidyselect ×1
vector ×1
while-loop ×1