有几种语言可以提供默认或逻辑或分配机制:
a = b || c;
a = b or c
a="${b:-$c}"
a = b ? b : c;
Run Code Online (Sandbox Code Playgroud)
到目前为止,我在Powershell Core中找到的唯一等价物是非常冗长:
$a = if ($b) { $b } else { $c }
Run Code Online (Sandbox Code Playgroud)
在某些情况下必须成为
$a = if ($b -ne $null) { $b } else { $c }
Run Code Online (Sandbox Code Playgroud)
有没有更好的选择[编辑:],这不会牺牲可读性?
powershell ternary-operator null-coalescing-operator powershell-core
在路由规划算法中,我试图根据到另一个节点的距离对节点列表执行过滤.我实际上是从粗略的场景图中提取列表.我使用术语"单元格"来指代简单场景图中的一个卷,我们从中获取了彼此接近的节点列表.
现在,我正在实现这个:
# SSCCE version of the core function
def nodes_in_range(src, cell, maxDist):
srcX, srcY, srcZ = src.x, src.y, src.z
maxDistSq = maxDist ** 2
for node in cell:
distSq = (node.x - srcX) ** 2
if distSq > maxDistSq: continue
distSq += (node.y - srcY) ** 2
if distSq > maxDistSq: continue
distSq += (node.z - srcZ) ** 2
if distSq <= maxDistSq:
yield node, distSq ** 0.5 # fast sqrt
from collections import namedtuple
class Node(namedtuple('Node', ('ID', 'x', …
Run Code Online (Sandbox Code Playgroud) 使用递归函数myPowerFunction(int p,int n,int和currentCallNumber)计算P的n次幂(p和n都是正整数).currentCallNumber是一个引用参数,用于存储到目前为止所进行的函数调用次数.myPowerFunction返回p的第n个幂.
int myPowerFunction(int p, int n, int &z)
{
z++;
if(n==1)return p;
else if(n==0)return 1;
else if(n%2==0)return myPowerFunction(p,n/2,z)*myPowerFunction(p,n/2,z);
else return myPowerFunction(p,n/2,z)*myPowerFunction(p,n/2,z)*p;
}
int main()
{
cout << myPowerFunction(3,4,1);
}
Run Code Online (Sandbox Code Playgroud) 根据非类型成员的类型,相同模板函数的变体是否有效?
template<typename T, unsigned int V>
void f(unsigned int& v) { v = V; }
template<typename T, bool B>
void f(bool& b) { b = B; }
Run Code Online (Sandbox Code Playgroud)
目的是让人能够打电话
unsigned int meaningOfLife;
f<sometype, 42>(meaningOfLife);
bool areYouAlive;
f<sometype, true>(areYouAlive);
Run Code Online (Sandbox Code Playgroud)
铿锵声和gcc声音沉默,但MSVC报道
warning C4305: 'specialization': truncation from 'int' to 'bool'
Run Code Online (Sandbox Code Playgroud)
我想避免要求指定常量类型:
f<sometype, bool, true>
Run Code Online (Sandbox Code Playgroud)
并希望确保常量值和目标值匹配.
---- mcve ----
#include <iostream>
template<unsigned int V>
void f(unsigned int& v) { v = V; }
template<bool B>
void f(bool& b) { b = B; }
int main() …
Run Code Online (Sandbox Code Playgroud) 我在某处读到c ++目标文件必须只通过g ++而不是gcc链接.这是真的吗?如果是,那么如何链接目标文件属于c,c ++和asm?
我想创建n个线程.然后传递一个结构,每个结构用数据填充该结构; 例如bool来跟踪线程是否已完成或是否已终止信号.
n = 5; // For testing.
pthread_t threads[n];
for(i=0; i<n; i++)
pthread_create(&threads[i], &thread_structs[i], &functionX);
Run Code Online (Sandbox Code Playgroud)
假设thread_structs已被malloced.
functionX()
通知功能内部没有参数.我应该为结构创建一个参数吗?或者我传递结构的地方还可以吗?
我如何指向刚刚传递给函数的结构?
我经常发现,当使用Pythonic套装时,Pythonic方式似乎不存在.
例如,做像dijkstra或*这样的事情:
openSet, closedSet = set(nodes), set(nodes)
while openSet:
walkSet, openSet = openSet, set()
for node in walkSet:
for dest in node.destinations():
if dest.weight() < constraint:
if dest not in closedSet:
closedSet.add(dest)
openSet.add(dest)
Run Code Online (Sandbox Code Playgroud)
这是一个虚弱的人为例子,重点是最后三行:
if not value in someSet:
someSet.add(value)
doAdditionalThings()
Run Code Online (Sandbox Code Playgroud)
鉴于Python的使用方式,例如,访问/使用dict的值,我本来希望能够做到:
try:
someSet.add(value)
except KeyError:
continue # well, that's ok then.
doAdditionalThings()
Run Code Online (Sandbox Code Playgroud)
作为一名C++程序员,我的皮肤爬行了一点,我甚至做不到:
if someSet.add(value):
# add wasn't blocked by the value already being present
doAdditionalThings()
Run Code Online (Sandbox Code Playgroud)
是否有更多Pythonic(如果可能更有效)的方式来使用这种类似防御的用法?
我在C++中有以下形式的字符串
string variable1="This is stackoverflow \"Here we go "1234" \u1234 ABC";
Run Code Online (Sandbox Code Playgroud)
现在在这个字符串中我想删除除字母(从a到b,A到B)和数字之外的所有字符.这样我的输出就变成了
variable1="This is stackoverflow Here we go 1234 u1234 ABC";
Run Code Online (Sandbox Code Playgroud)
我试图使用指针检查每个字符,但发现效率非常低.有没有一种使用C++/C实现相同目的的有效方法?
在我的TicTacToe游戏中,我遇到了虚拟功能的问题.以下代码在Dev C++中引发错误:"class std :: auto_ptr"没有名为'makeAMove'的成员.
根据错误,问题与makeAMove函数有关,但我看不出有什么问题.另外,我应该提一下,我正在使用已弃用的auto_ptr函数而不是unique_ptr,因为显然教师对我的代码进行分级的辅助工具没有符合C++ 11的编译器.
现在playGame和makeAMove函数都没有做任何事情,但我想在继续之前找出导致错误的原因.
谢谢你的任何建议.
这是相关的代码:
Game.h(充当游戏的控制器)
#include "Player.h"
#include "AIPlayer.h"
#include "HumanPlayer.h"
#include <memory>
class Game
{
public:
Game(unsigned int players)
{
if (players == 1)
{
player1.reset (new HumanPlayer());
player2.reset (new AIPlayer());
}
else
{
player1.reset (new HumanPlayer());
player2.reset (new HumanPlayer());
}
}
void playGame()
{
player1.makeAMove();
}
private:
std::auto_ptr<Player> player1; // pointer to a Player object (C++11 has more secure unique_ptr)
std::auto_ptr<Player> player2; // pointer to a Player object
};
Run Code Online (Sandbox Code Playgroud)
Player.h(HumanPlayer.h和AIPlayer.h的基类)
class Player …
Run Code Online (Sandbox Code Playgroud) 我的程序计算字符串中每个数字从0到9的出现次数.在我看来,我已经完成了所有事情,但问题仍然存在.
int main(){
string word = "23456745";
int* ReturnArray = count(word);
for(int i =0; i < 10; i++){
cout << i << ": " << ReturnArray[i] << " \n";
}
delete [] ReturnArray;
return 0;
}
int* count(const string& s){
int length = s.length();
int* array = new int(10);
int counter =0;
for(int j = 0 ; j < 10 ; j++) {
for(int i = 0 ; i < length ; i++) {
char character = s[i];
int value …
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
main ()
{
printf("Hello world !");
*(int *)(0) = 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译此代码并运行时,它没有将字符串打印到控制台.在那之后,我修改了一点:
main ()
{
printf("Hello world !\n");
*(int *)(0) = 0;
}
Run Code Online (Sandbox Code Playgroud)
而且,它工作!
我认为背后的奥秘是*(int *)(0) = 0;
但不知道为什么!
PS.我正在使用gcc 4.8.2进行编译.
c++ ×6
c ×2
python ×2
auto-ptr ×1
dynamic ×1
g++ ×1
gcc ×1
memory ×1
numpy ×1
powershell ×1
pthreads ×1
python-3.4 ×1
python-3.x ×1
templates ×1