在Bash中,测试数组是否包含某个值的最简单方法是什么?
编辑:在答案和评论的帮助下,经过一些测试后,我想出了这个:
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
}
echo "n"
return 1
}
A=("one" "two" "three four")
if [ $(contains "${A[@]}" "one") == "y" ]; then
echo "contains one"
fi
if [ $(contains "${A[@]}" "three") == "y" ]; then
echo "contains three"
fi
Run Code Online (Sandbox Code Playgroud)
我不确定它是否是最佳解决方案,但似乎有效.
有没有一种简单的方法来计算PowerShell中命令的执行时间,比如Linux中的'time'命令?
我想出了这个:
$s=Get-Date; .\do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds
Run Code Online (Sandbox Code Playgroud)
但我想要更简单的东西
time .\do_something.ps1
Run Code Online (Sandbox Code Playgroud) using
在(可能)null对象上使用该语句是否安全?
请考虑以下示例:
class Test {
IDisposable GetObject(string name) {
// returns null if not found
}
void DoSomething() {
using (IDisposable x = GetObject("invalid name")) {
if (x != null) {
// etc...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否保证Dispose
只有在对象不为空时才会被调用,而且我不会得到一个NullReferenceException
?
假设有这样的事情:
#include <map>
int main(){
std::map<int,int> m;
m[1] = 2;
m[2] = 4;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够从gdb检查运行该程序的地图的内容.
如果我尝试使用下标运算符,我得到:
(gdb) p m[1]
Attempt to take address of value not located in memory.
Run Code Online (Sandbox Code Playgroud)
使用find方法不会产生更好的结果:
(gdb) p m.find(1)
Cannot evaluate function -- may be inlined
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
在C#中,使用System.Object
代码而不仅仅是object
,或者System.String
不是string
等等有什么区别?或者只是风格问题?
是否有理由说一种形式对另一种形式更有利?
我正在使用Vim,我想用一个长字符串替换一些占位符文本,该字符串跨越多行,这些行已经写入文件中的其他位置.
是否可以用寄存器的内容替换模式?就像是
:%s/foo/<contents of register A>
Run Code Online (Sandbox Code Playgroud)
否则,是否可以用一系列线代替?就像是
:%s/foo/<content of lines from 10 to 15>
Run Code Online (Sandbox Code Playgroud) 我在C++中定义了一个接口,即一个只包含纯虚函数的类.
我想明确禁止接口的用户通过指向接口的指针删除对象,所以我为接口声明了一个受保护的非虚拟析构函数,如:
class ITest{
public:
virtual void doSomething() = 0;
protected:
~ITest(){}
};
void someFunction(ITest * test){
test->doSomething(); // ok
// deleting object is not allowed
// delete test;
}
Run Code Online (Sandbox Code Playgroud)
GNU编译器给我一个警告说:
class'ITest'具有虚函数但非虚析构函数
一旦析构函数受到保护,虚拟或非虚拟有什么区别?
你认为这个警告可以被安全地忽略或沉默吗?
我正在尝试从控制台应用程序调用Web服务,我需要为客户端提供一个System.Net.NetworkCredential
对象.
是否可以为NetworkCredential
启动应用程序的用户创建一个对象而不提示输入用户名/密码?
在bash中
echo ${!X*}
Run Code Online (Sandbox Code Playgroud)
将打印名称以"X"开头的变量的所有名称.
是否可以使用任意模式获得相同的结果,例如获取名称在任何位置包含"X"的变量的所有名称?
我有两个字符串:
string1 = "theater is small";
string2 = "The small thing in the world";
Run Code Online (Sandbox Code Playgroud)
我需要检查字符串中是否存在字符串"the".
我可以使用contains函数,但它可以做一个完整的单词匹配吗?即它不应该与string1的"剧院"相匹配!
c# ×4
bash ×2
c++ ×2
arrays ×1
coding-style ×1
gcc ×1
gdb ×1
idisposable ×1
map ×1
powershell ×1
replace ×1
scripting ×1
stl ×1
substitution ×1
time ×1
using ×1
variables ×1
vim ×1
web-services ×1