我正在努力学会使用getopt_long.从维基百科,我看到代码
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h> /* for getopt_long; POSIX standard getopt is in unistd.h */
int main (int argc, char **argv) {
int c;
int digit_optind = 0;
int aopt = 0, bopt = 0;
char *copt = 0, *dopt = 0;
static struct option long_options[] = {
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 1, …Run Code Online (Sandbox Code Playgroud) c parameter-passing getopt getopt-long command-line-arguments
我想从文件中读取file.txt索引n低于与给定正则表达式匹配的行的所有行regex.例如文件
hello my friend
foo
_bar_
I love this bar
poof
kouki
splash in the water
bar
Run Code Online (Sandbox Code Playgroud)
如果regex=bar和n=2,那么我们想读
hello my friend
foo
kouki
Run Code Online (Sandbox Code Playgroud)
我找到了解决这个问题的方法
sed -n `grep -n bar file.txt | awk -F ":" '{print ($1 - 2)}' | tr '\n' 'X'
| sed 's+X+p;+g' | sed 's/.$//'` < file.txt
Run Code Online (Sandbox Code Playgroud)
是否有更好(更快,更容易阅读)的解决方案?
(我对这个问题的目标纯粹是教育性的)
编码
using namespace std;
class A
{
private:
vector<int> a;
public:
A(vector<int> x):a(x){}
string toString()
{
string s;
for (auto& element : a)
{
s += to_string(element) + " ";
}
return s;
}
};
int main()
{
A a1({1,2,3});
A a2({11,12,13});
cout << "a1 = " << a1.toString() << "\n";
cout << "a2 = " << a2.toString() << "\n";
swap(a1,a2);
cout << "a1 = " << a1.toString() << "\n";
cout << "a2 = " << a2.toString() << "\n";
return …Run Code Online (Sandbox Code Playgroud) 怎么来
input = c("a", "b", "c")
Run Code Online (Sandbox Code Playgroud)
至
output = c(quote(a), quote(b), quote(c))
Run Code Online (Sandbox Code Playgroud)
自动?
我有一个6行矩阵和一个长度为6的向量,我想用它来重新组织我的矩阵.
我的新矩阵的第一行应该包含我的向量的第一个数值的位置行.
我的新矩阵的第二行应该包含我的向量的第二个数值的位置行.
这是一个例子:
> Matrix <- matrix(rnorm(30),nrow=6,ncol=5)
> Vector <- c(1,3,6,2,4,5)
Run Code Online (Sandbox Code Playgroud)
操作之后的实际第一行将位于第一行位置.在操作之后,实际的第二行将位于第三行位置.在操作之后,实际的第三行将位于第六行位置.等等....
我希望我的解释清楚得多!
为了使我的问题更清楚,我做了这个编辑
编辑:
> Matrix=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3,byrow=T)
> Matrix
[,1][,2][,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> Vector=c(2,3,1)
Run Code Online (Sandbox Code Playgroud)
我想重新组织行以获得这个:
> NewMatrix=matrix(c(4,5,6,7,8,9,1,2,3),byrow=T)
> NewMatrix
[,1][,2][,3]
[1,] 4 5 6
[2,] 7 8 9
[3,] 1 2 3
Run Code Online (Sandbox Code Playgroud)
第2行现在位于第1位,第3行现在位于第2位,第1行现在位于第3位.
我正在使用R for mac在.R文件上编写所有脚本.这对我来说很方便,因为有颜色可以突出显示命令的类型.
我在#符号后面有很多注释,当我忘记我的脚本的含义时会很有用,但是它们会使我的脚本变得模糊,以至于找到给定的命令行变得更加困难.
有没有办法隐藏和显示这些评论?(使用我正在使用的程序或另一个程序).作为编写R脚本的最佳程序,您会建议什么?
非常感谢 !
我正在寻找一个TRUE以给定概率返回的函数。就像是:
> proba = 2/3
> function(proba)
Run Code Online (Sandbox Code Playgroud)
它TRUE以 2/3 的概率返回(或 1),以FALSE1/3 的概率返回(或 0)
我能想到的唯一计算方法是:
> sample(c(rep(1,ceiling(proba*100)),rep(0,ceiling((1-proba)*100))),1)
Run Code Online (Sandbox Code Playgroud)
但它只给出了一个近似值(而且看起来并不好看!),因为它只能处理小数位数有限的值。
我有以下类型的列表:
class Any(object):
def __init__(self,a,b):
self.a=a
self.b=b
l=[Any(1,3),Any(2,4),Any(1,2),Any(None,6),Any('hello',6), Any(1,'ChuckNorris'),Any(1,2)]
Run Code Online (Sandbox Code Playgroud)
l是一个只包含实例的列表Any.我想找到第一个属性a等于'None'的位置.
由于我的列表很长,算法不应该探索整个列表,但是一旦找到条件(在我的示例中,属性a等于None),它就应该停止.
在上面的例子中,这个算法的答案应该是3.
在Python中,我们有一个列表列表.例如:
a1 = [[1,4,3,5,6],[6,7,5,3,12],[1,6,4,1,2],[1,9,4,2,1]]
Run Code Online (Sandbox Code Playgroud)
要么
a2 = [[4,3,5,6],[6,7,5,12],[1,6,4,2],[1,9,2,1],[3,2,5,11]]
Run Code Online (Sandbox Code Playgroud)
a内部列表的长度和长度都可以变化.但是所有内部列表总是具有相同的长度.
我想将一些函数应用于由每个列表的第n个元素组成的所有列表.
考虑a2例如,我想的函数迭代地应用到列表[4,6,1,1,3](第一要素列表),然后到列表[3,7,6,9,2](第二个元素列表)等...
这个想法类似于功能:
map(a2, my_function)
Run Code Online (Sandbox Code Playgroud)
除了my_function不应该应用于每个内部列表,而应该应用于每个第n个元素列表.
希望有道理!
执行这样的事情的最pythonic方式是什么?
考虑这个小代码
#include <stdio.h>
#include <stdlib.h>
void* foo1(double bar)
{
double s2 = 0.5;
double s1 = bar/s2;
double toreturn[2] = {s1,s2};
printf("Outside main. Second value: %f\n", toreturn[1]); //this line is commented out for the second output. See below text
return toreturn;
}
int main()
{
void* (*foo)(double) = NULL;
foo = &foo1;
double bar = 2.12;
double* resp = foo(bar);
printf("In main. First value: %f\n", resp[0]);
printf("In main. Second value: %f\n", resp[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码输出
Outside main. Second value: …Run Code Online (Sandbox Code Playgroud)