我需要一个针对以下情况的建议 - 我几个小时都无法弄明白:如何通过多个seq.容器大小相同(这里:两个向量)的简单方法?
int main() {
int size = 3;
std::vector<int> v1{ 1, 2, 3 }, v2{ 6, 4, 2 };
// old-fashioned - ok
for (int i = 0; i < size; i++) {
std::cout << v1[i] << " " << v2[i] << std::endl;
}
// would like to do the same as above with auto range-for loop
// something like this - which would be fine for ONE vector.
// But this does not work. Do I need …Run Code Online (Sandbox Code Playgroud) 我目前陷入困境,试图为以下列表理解问题找到一个很好的解决方案:
在两个列表中很容易找到具有相同索引的相等值,例如
>>> vec1 = [3,2,1,4,5,6,7]
>>> vec2 = [1,2,3,3,5,6,9]
>>> [a for a, b in zip(vec1, vec2) if a == b]
[2,5,6]
Run Code Online (Sandbox Code Playgroud)
但是,我只需要列表中出现这些匹配的索引,而不是值本身.使用上面的例子,我想要的输出是:[1,4,5]
我修好了但我只能想到一个"多线"的解决方案.有谁知道我怎么能以更Pythonic的方式找到索引?
一小段代码让我发疯,但希望你可以阻止我跳出窗外.看这里:
#include <iostream>
#include <cstdint>
int main()
{
int8_t i = 65;
int8_t j;
std::cout << "i = " << i << std::endl; // the 'A' is ok, same as uchar
std::cout << "Now type in a value for j (use 65 again): " << std::endl;
std::cin >> j;
std::cout << "j = " << j << std::endl;
if (i != j)
std::cout << "What is going on here?????" << std::endl;
else
std::cout << "Everything ok." << std::endl;
return 0; …Run Code Online (Sandbox Code Playgroud)