我使用R绘制一个简单的线性回归.我想将该图像保存为PNG或JPEG,是否可以自动执行?(通过代码)
有两个不同的问题:首先,我已经在我的显示器上查看情节了,我想保存原样.其次,我还没有生成情节,但是当我执行绘图代码时,我想直接将它保存到磁盘.
是否可以通过lambda表达式中的const引用进行捕获?
我希望下面标记的作业失败,例如:
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string strings[] =
{
"hello",
"world"
};
static const size_t num_strings = sizeof(strings)/sizeof(strings[0]);
string best_string = "foo";
for_each( &strings[0], &strings[num_strings], [&best_string](const string& s)
{
best_string = s; // this should fail
}
);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更新:由于这是一个老问题,如果C++ 14中有设施来帮助解决这个问题,那么更新它可能会更好.C++ 14中的扩展是否允许我们通过const引用捕获非const对象?(2015年8月)
好吧,我想我们都同意以下代码所发生的事情是未定义的,具体取决于传递的内容,
void deleteForMe(int* pointer)
{
delete[] pointer;
}
Run Code Online (Sandbox Code Playgroud)
指针可以是各种不同的东西,因此delete[]对它执行无条件是未定义的.但是,让我们假设我们确实传递了一个数组指针,
int main()
{
int* arr = new int[5];
deleteForMe(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在这种情况下,指针是一个数组,谁知道这个?我的意思是,从语言/编译器的角度来看,它不知道arr数组指针是否指向单个int的指针.哎呀,它甚至不知道是否arr是动态创建的.但是,如果我做以下事情,
int main()
{
int* num = new int(1);
deleteForMe(num);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
操作系统足够聪明,只能删除一个int,而不是通过删除超出该点的其余内存来进行某种类型的"杀戮狂欢"(与strlen非\0终结字符串形成对比- 它将一直持续到它点击0).
那么他们的工作是记住这些东西吗?操作系统是否在后台保留某种类型的记录?(我的意思是,我意识到我开始这篇文章时说过发生的事情是未定义的,但事实是,'杀戮狂欢'的情况不会发生,所以因此在实际世界中有人记得.)
我想打印整个数据帧,但我不想打印索引
另外,一列是datetime类型,我只想打印时间,而不是日期.
数据框如下所示:
User ID Enter Time Activity Number
0 123 2014-07-08 00:09:00 1411
1 123 2014-07-08 00:18:00 893
2 123 2014-07-08 00:49:00 1041
Run Code Online (Sandbox Code Playgroud)
我希望它打印为
User ID Enter Time Activity Number
123 00:09:00 1411
123 00:18:00 893
123 00:49:00 1041
Run Code Online (Sandbox Code Playgroud) 我正在通过一个例如pgm来创建一个make文件.
http://mrbook.org/tutorials/make/
我的文件夹eg_make_creation包含以下文件,
desktop:~/eg_make_creation$ ls
factorial.c functions.h hello hello.c main.c Makefile
Run Code Online (Sandbox Code Playgroud)
# I am a comment, and I want to say that the variable CC will be
# the compiler to use.
CC=gcc
# Hwy!, I am comment no.2. I want to say that CFLAGS will be the
#options I'll pass to the compiler
CFLAGS=-c -Wall
all:hello
hello:main.o factorial.o hello.o
$(CC) main.o factorial.o hello.o -o hello
main.o:main.c
$(CC) $(CFLAGS) main.c
factorial.o:factorial.c
$(CC) $(CFLAGS) factorial.c
hello.o:hello.c
$(CC) $(CFLAGS) hello.c
clean: …Run Code Online (Sandbox Code Playgroud) 具体来说我很感兴趣istream& getline ( istream& is, string& str );.是否有ifstream构造函数的选项告诉它将所有换行编码转换为引擎盖下的'\n'?我希望能够打电话getline并优雅地处理所有行结尾.
更新:为了澄清,我希望能够编写几乎可以在任何地方编译的代码,并且几乎可以从任何地方获取输入.包括'\ r'没有'\n'的稀有文件.最大限度地减少软件用户的不便.
解决这个问题很容易,但我仍然对标准中正确处理所有文本文件格式的方式感到好奇.
getline读取一个完整的行,直到'\n',成为一个字符串.'\n'从流中消耗,但getline不包含在字符串中.到目前为止这很好,但是在'\n'之前可能会有一个'\ r'被包含在字符串中.
有三种类型的行结尾的文本文件中看到:"\n"是Unix机器上的常规结尾,"\ r"是在旧的Mac操作系统使用,Windows使用一对(我认为),"\ r"跟随'\n'.
问题是getline在字符串末尾留下'\ r'.
ifstream f("a_text_file_of_unknown_origin");
string line;
getline(f, line);
if(!f.fail()) { // a non-empty line was read
// BUT, there might be an '\r' at the end now.
}
Run Code Online (Sandbox Code Playgroud)
编辑感谢Neil指出这f.good()不是我想要的.!f.fail()是我想要的.
我可以自己手动删除它(请参阅此问题的编辑),这对于Windows文本文件很容易.但是我担心有人会输入一个只包含'\ r'的文件.在这种情况下,我认为getline将消耗整个文件,认为它是一行!
..那甚至不考虑Unicode :-)
..也许Boost有一种很好的方式从任何文本文件类型一次消耗一行?
编辑我正在使用它来处理Windows文件,但我仍然觉得我不应该这样做!这不会为'\ r'专用文件分叉.
if(!line.empty() && *line.rbegin() == '\r') {
line.erase( line.length()-1, 1);
}
Run Code Online (Sandbox Code Playgroud) 有这样的代码:
#include <iostream>
int main(){
unsigned int* wsk2 = new unsigned int(5);
std::cout << "wsk2: " << wsk2 << " " << *wsk2 << std::endl;
delete wsk2;
wsk2 = new unsigned int;
std::cout << "wsk2: " << wsk2 << " " << *wsk2 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
wsk2: 0x928e008 5
wsk2: 0x928e008 0
Run Code Online (Sandbox Code Playgroud)
我读过new没有用零初始化内存.但似乎它确实如此.它是如何工作的?
怎么能*i和u.i这个代码打印不同的数字,即使i被定义为int *i = &u.i;?我只能假设我在这里触发UB,但我看不清楚到底是怎么回事.
(如果我选择'C'作为语言,ideone demo会复制.但正如@ 2501指出的那样,如果'C99严格'是语言,那就不行了.但话又说回来,我得到了问题gcc-5.3.0 -std=c99!)
// gcc -fstrict-aliasing -std=c99 -O2
union
{
int i;
short s;
} u;
int * i = &u.i;
short * s = &u.s;
int main()
{
*i = 2;
*s = 100;
printf(" *i = %d\n", *i); // prints 2
printf("u.i = %d\n", u.i); // prints 100
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(gcc 5.3.0,with -fstrict-aliasing -std=c99 -O2,with with -std=c11 …
语言R让我困惑.实体具有模式和类,但即使这不足以完全描述实体.
这个答案说
在R中,每个"对象"都有一个模式和一个类.
所以我做了这些实验:
> class(3)
[1] "numeric"
> mode(3)
[1] "numeric"
> typeof(3)
[1] "double"
Run Code Online (Sandbox Code Playgroud)
到目前为止公平,但后来我传入了一个向量:
> mode(c(1,2))
[1] "numeric"
> class(c(1,2))
[1] "numeric"
> typeof(c(1,2))
[1] "double"
Run Code Online (Sandbox Code Playgroud)
这没有意义.当然,整数向量应该具有与单个整数不同的类或不同的模式吗?我的问题是:
更新:显然,文字3只是长度为1的向量.没有标量.好吧但是......我试过mode("string")了"character",让我觉得字符串是一个字符向量.但如果这是真的,那么这应该是真的,但事实并非如此!c('h','i') == "hi"
我想添加在编译期间检查结构大小的代码,以确保它是预定义的大小.例如,当我移植此代码或在编译期间添加/删除结构中的项时,我想确保此结构的大小为1024字节:
#pack(1)
struct mystruct
{
int item1;
int item2[100];
char item3[4];
char item5;
char padding[615];
}
Run Code Online (Sandbox Code Playgroud)
我知道如何在运行时使用如下代码执行此操作:
if(sizeof(mystruct) != 1024)
{
throw exception("Size is not correct");
}
Run Code Online (Sandbox Code Playgroud)
但如果我在运行期间这样做,那将浪费处理.我需要在编译期间执行此操作.
如何在编译期间执行此操作?