我想将一个以上的项目从一个列表移动到另一个列表.
list1 = ['2D',' ',' ',' ',' ',' ',' ',' ',' ']
list2 = ['XX','XX','5D','4S','3D',' ',' ',' ',' ']
list3 = ['XX','XX','XX','8C','7H','6C',' ',' ',' ']
Run Code Online (Sandbox Code Playgroud)
在上面的代码中' '
是一个双重空间
我想能够移动'5D','4S','3D'
从list2
到'8C','7H','6C'
在list3
.
我已经尝试了下面的代码,但它不起作用.
list1 = ['2D',' ',' ',' ',' ',' ',' ',' ',' ']
list2 = ['XX','XX','5D','4S','3D',' ',' ',' ',' ']
list3 = ['XX','XX','XX','8C','7H','6C',' ',' ',' ']
items_to_be_moved = list2[list2.index('XX')+2 : list2.index(' ')]
list3[list3.index(' ')] = items_to_be_moved
del list2[list2.index('XX')+2 : list2.index(' ')]
print('list2',list2)
print('list3',list3) …
Run Code Online (Sandbox Code Playgroud) 当我想为向量声明迭代器时,为什么需要使用类型名?
例如:
typename vector<T>::iterator i;
Run Code Online (Sandbox Code Playgroud)
如果我删除关键字,typename
那么该程序根本无法运行。我写的内容是:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <class T>
class MyClass
{
private:
vector<T> array;
public:
MyClass ( T * begin,int n ) : array(n)
{
copy( begin, begin + n, array.begin());
}
void List()
{
typename vector<T>::iterator i;
for( i = array.begin(); i != array.end(); ++i )
cout << * i << "," ;
}
};
int main()
{
string array[4] = { "Tom","Jack","Mary","John"};
MyClass<string>obj(array,4);
obj.List();
return 0; …
Run Code Online (Sandbox Code Playgroud)