鉴于:
x = ['w', 'e', 's', 's', 's', 'z','z', 's']
Run Code Online (Sandbox Code Playgroud)
每次出现都s
出现在以下指数:
1st:2
2nd:3
3rd:4
4th:7
如果我这样做,x.index('s')
我将得到第一个索引.
我如何得到第四个指数s
?
所以我有一个for循环.
>>> row = [5, 2, 5, 4, 2, 2, 5, 5, 5, 2]
>>> for i in row:
if i == 5:
print(row.index(i))
if i == 2:
print(row.index(i))
Run Code Online (Sandbox Code Playgroud)
OUTPUT
0
1
0
1
1
0
0
0
1
Run Code Online (Sandbox Code Playgroud)
我想得到:0 1 2 4 5 6 7 8 9
.换句话说,我想得到i
我正在for
循环中看到的索引,如果它是= 5或2 ...任何简单的方法来做到这一点?
当我们打电话
delete[] array; // array is a pointer to an array
Run Code Online (Sandbox Code Playgroud)
要么
delete[] vector; // vector is a pointer to a vector
Run Code Online (Sandbox Code Playgroud)
假设这些是链表头指针的数组/向量:这两个语句是否为它们中的每个项调用析构函数?或者我们应该遍历它们,删除每个项目,然后删除[]数组或向量本身?
让我更具体一点,如果数组中包含一个带有析构函数的头指针,则会删除它的下一个节点或指向它.
我目前正在研究直方图,而且我是C++的新手,所以我不知道是否有一种简单的方法可以做到这一点,有人可以帮忙吗?
数字始终在-1到99之间.我想返回第一个数字.我知道可以使用%返回最后一位数字,但找不到返回第一位数字的方法.
Examples:
98 --> 9
87 --> 8
24 --> 2
10 --> 1
==For anything under 9 I would like to return a 0==
9 --> 0
5 --> 0
0 --> 0
-1 --> 0
Run Code Online (Sandbox Code Playgroud) 根据我的理解,标题防护用于避免意外包含多次.但是,当我多次包含一个类时,我仍然会遇到重定义错误.头卫不应该照顾这个吗?//animal.h
#ifndef ANIMAL_H
#define AMIMAL_H
class Animal {};
#endif
Run Code Online (Sandbox Code Playgroud)
//main.cpp
#include"animal.h"
#include"animal.h"
Run Code Online (Sandbox Code Playgroud)
错误C2011:'动物':'类'类型重新定义
使用Normal指针,它很简单:
int* p = new int;
int* x = new int;
p=x;
Run Code Online (Sandbox Code Playgroud)
但是有了共享,有:swap,reset等等
std::shared_ptr<int> x = NULL;
std::shared_ptr<int> y = NULL;
Run Code Online (Sandbox Code Playgroud)
我知道重置用于"新"
x.reset(new int(5));
Run Code Online (Sandbox Code Playgroud)
如果我希望x和y都指向这个新的整数5,我是否使用reset或swap或=?我真的很困惑.
y = x;
y.swap(x);
y.reset(x);
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下吗?
谢谢.
c++ ×4
python ×2
class ×1
histogram ×1
indexing ×1
inheritance ×1
iterable ×1
memory-leaks ×1
numbers ×1
pointers ×1
redefinition ×1
shared-ptr ×1