小编Pet*_*ood的帖子

为什么([1,0] == True中的1)评估为False?

当我看到这个问题的答案时,我发现我不明白自己的答案.

我真的不明白这是如何被解析的.为什么第二个示例返回False?

>>> 1 in [1,0]             # This is expected
True
>>> 1 in [1,0] == True     # This is strange
False
>>> (1 in [1,0]) == True   # This is what I wanted it to be
True
>>> 1 in ([1,0] == True)   # But it's not just a precedence issue!
                           # It did not raise an exception on the second example.

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    1 in ([1,0] == …
Run Code Online (Sandbox Code Playgroud)

python syntax operator-precedence

149
推荐指数
1
解决办法
4630
查看次数

检查迭代器是否有效

有没有办法检查迭代器(无论是来自向量,列表,双端队列......)是否(仍)可解除引用,即未被无效?

我一直在使用try- catch,但是有更直接的方法吗?

示例:(不起作用)

list<int> l;
for (i = 1; i<10; i++) {
    l.push_back(i * 10);
}

itd = l.begin();
itd++;
if (something) {
    l.erase(itd);
}

/* now, in other place.. check if itd points to somewhere meaningful */
if (itd != l.end())
{
    //  blablabla
}
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl dereference

63
推荐指数
5
解决办法
7万
查看次数

否定数字的最快方法

我今天早上在这里思考,最好的方法是将一些积极转为负面,从消极转为正面,当然,最简单的方法可能是:

int a = 10;
a = a*(-1);
Run Code Online (Sandbox Code Playgroud)

要么

int a = 10;
a = -a;
Run Code Online (Sandbox Code Playgroud)

但是,我想,我接着这样做,使用命令shift和指针......真的可以使用命令移位运算符和内存来改变值的符号吗?

c c++ intel visual-c++-2012

33
推荐指数
4
解决办法
5万
查看次数

写出^ 3 + b ^ 3 = c ^ 3 + d ^ 3的所有解

写出a ^ 3 + b ^ 3 = c ^ 3 + d ^ 3的所有解,其中a,b,c,d位于[0,10 ^ 5]之间.

这是一个面试问题,我完全无能为力.

我认为优先队列至少要迭代ab值.一些提示会很棒,会尝试从那里开始.

algorithm

25
推荐指数
3
解决办法
2万
查看次数

如何从std :: vector <string>构造一个std :: string?

我想建立std::string一个std::vector<std::string>.

我可以使用std::stringsteam,但想象有一个更短的方式:

std::string string_from_vector(const std::vector<std::string> &pieces) {
  std::stringstream ss;

  for(std::vector<std::string>::const_iterator itr = pieces.begin();
      itr != pieces.end();
      ++itr) {
    ss << *itr;
  }

  return ss.str();
}
Run Code Online (Sandbox Code Playgroud)

我怎么可能这样做?

c++ stl string-concatenation stringstream stdstring

24
推荐指数
5
解决办法
4万
查看次数

继承和方法重载

为什么C++编译器会出现此错误?为什么我可以从B访问lol(),但无法访问rofl()[不带参数].捕获量在哪里?

class A
{
public:
   void lol(void) {}
   void rofl(void) { return rofl(0);}
   virtual void rofl(int x) {}
};

class B : public A
{
public:
   virtual void rofl(int x) {}
};

int _tmain(int argc, _TCHAR* argv[])
{
    A a;
   a.lol();
   a.rofl(1);
   a.rofl();

   B  b;
   b.lol();
   b.rofl(1);    
   b.rofl(); //ERROR -> B::rofl function does not take 0 arguments


   return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance overriding overloading

18
推荐指数
1
解决办法
9624
查看次数

有没有办法在Visual Studio中捕获stderr和stdout?

有没有办法在Visual Studio中捕获stdout和stderr?例如,当我使用时

cout <<"Hello world!"<< endl;
Run Code Online (Sandbox Code Playgroud)

出现黑色窗口并消失.它太快了,我看不到它.IDE中有一个输出部分,但它只允许我从构建中选择显示输出,而不选择stdout.

作弊解决方案可能会打电话

system("pause");
Run Code Online (Sandbox Code Playgroud)

但听起来不对.我在选项中搜索但我找不到一个项目.

任何人有任何想法?谢谢.我刚开始使用VS而且我之前在Linux上.

c++ ide command-line stdout visual-studio-2010

17
推荐指数
2
解决办法
2万
查看次数

寻求优雅的Python骰子迭代

是否有一种优雅的方式来迭代最多五个骰子的可能骰子?

我想替换这个hacky Python:

self.rolls[0] = [str(a) for a in range(1,7)]
self.rolls[1] = [''.join([str(a), str(b)])
                 for a in range(1, 7)
                 for b in range(1, 7)
                 if a <= b]
self.rolls[2] = [''.join([str(a), str(b), str(c)])
                 for a in range(1, 7)
                 for b in range(1, 7)
                 for c in range(1, 7)
                 if a <= b <= c]
self.rolls[3] = [''.join([str(a), str(b), str(c), str(d)])
                 for a in range(1, 7)
                 for b in range(1, 7)
                 for c in range(1, 7)
                 for d in range(1, 7) …
Run Code Online (Sandbox Code Playgroud)

python

15
推荐指数
1
解决办法
847
查看次数

open()如何使用和不使用`with`?

我想写一个类似的函数open.我希望能够用它来呼叫它with,但也没有with.

当我使用时contextlib.contextmanager,它使我的功能正常工作with:

@contextmanager
def versioned(file_path, mode):
    version = calculate_version(file_path, mode)
    versioned_file = open(file_path, mode)
    yield versioned_file
    versioned_file.close()
Run Code Online (Sandbox Code Playgroud)

所以,我这样使用它:

with versioned('file.txt', 'r') as versioned_file:
    versioned_file.write(...)
Run Code Online (Sandbox Code Playgroud)

如何在没有的情况下使用它with:

versioned_file = versioned('file.txt', 'r')
versioned_file.write(...)
versioned_file.close()
Run Code Online (Sandbox Code Playgroud)

它抱怨说:

AttributeError: 'GeneratorContextManager' object has no attribute 'write'
Run Code Online (Sandbox Code Playgroud)

python

13
推荐指数
2
解决办法
3333
查看次数

如何知道使用pip安装了哪些软件包

我在Windows中安装了Python,用于pip安装很多东西.

我如何知道我安装的软件包pip

python windows pip

12
推荐指数
1
解决办法
2万
查看次数