在C++中,如何创建多维std::array?我试过这个:
std::array<std::array<int, 3>, 3> arr = {{5, 8, 2}, {8, 3, 1}, {5, 3, 9}};
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我做错了什么,如何解决这个问题?
如果我有这样的文本文件:
Hello World
How are you?
Bye World
Run Code Online (Sandbox Code Playgroud)
我怎么把它读成像这样的多维数组:
[["Hello", "World"],
["How", "are", "you?"],
["Bye" "World"]]
Run Code Online (Sandbox Code Playgroud)
我试过了:
textFile = open("textFile.txt")
lines = textFile.readlines()
for line in lines:
line = lines.split(" ")
Run Code Online (Sandbox Code Playgroud)
但它只会返回:
["Hello World\n", "How are you?\n", "Bye World"]
Run Code Online (Sandbox Code Playgroud)
如何将文件读入多维数组?
在禅宗的Python中,蒂姆·彼得斯说明了这一点Flat is better than nested..如果我已正确理解,那么在Python中,这个:
<statement-1> if <condition> else <statement-2>
Run Code Online (Sandbox Code Playgroud)
通常比这更受欢迎:
if <condition>:
<statement-1>
else:
<statement-2>
Run Code Online (Sandbox Code Playgroud)
但是,在其他语言中,我被告知不要嵌套三元运算符,而是使用传统的运算符 if...else.那么,我的问题是我应该使用这个:
(<statement-1> if <condition-1> else <statement-2>) if <condition-2> else <statement-3>
Run Code Online (Sandbox Code Playgroud)
要么
if <condition-2>:
if <condition-1>:
<statement-1>
else:
<statement-2>
else:
<statement-3>
Run Code Online (Sandbox Code Playgroud)
?特别是如果陈述和条件很长,第一行需要拆分?
我有这两个类:
class Hand
{
public:
int getTotal();
std::vector<Card>& getCards();
void add(Card& card);
void clear();
private:
std::vector<Card> cards;
};
class Deck : public Hand
{
public:
void rePopulate();
void shuffle();
void deal(Hand& hand);
};
Run Code Online (Sandbox Code Playgroud)
当shuffle()函数声明如下:
void Deck::shuffle()
{
std::random_shuffle(cards.begin(), cards.end());
}
Run Code Online (Sandbox Code Playgroud)
但是,这会返回以下错误:
'Hand::cards' : cannot access private member declared in class 'Hand'
Run Code Online (Sandbox Code Playgroud)
我是否应该只包含一个函数,例如std::vector<Card>& getCards()或者 是否有另一种方法来避免错误。
我是C++的新手,我创建了这个函数:
bool guessWord(string compWord)
{
cout << "Guess a letter: ";
string userLetter;
cin >> userLetter;
for (unsigned int x = 0; x < compWord.length(); x++)
{
string compLetter = compWord[x];
if (compLetter == userLetter)
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
但它回归到以下error: invalid conversion from 'char' to 'const char*' [-fpermissive].任何人都可以帮我理解这意味着什么?
我在HTML5中有以下导航栏:
<div id = 'nav'>
<ul>
<li>
<a href = ''>HOME</a>
</li>
<li class = 'final_nav'>
<a href = ''>FAQ</a>
</li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
用这个CSS设计:
#nav {
border: 1px solid #000;
padding: 0.1px;
background-color: #E5E5E5;
}
#nav li {
border-left: 1px solid #000;
padding: 20px;
display: inline;
}
.final_nav {
border-right: 1px solid #000;
}
#nav a {
display: block-inline;
}
Run Code Online (Sandbox Code Playgroud)
但左右边框与顶部和底部边框不对齐:
为什么是这样?
谢谢
我已经读过这是在Python中实现抽象类的最佳方法(而不是abc模块):
class Animal(object):
def speak(self):
raise NotImplementedError()
class Duck(Animal):
def speak(self):
print("quack")
class Cat(Animal):
def speak(self):
print("meow")
Run Code Online (Sandbox Code Playgroud)
但是,我应该使用抽象类吗?我应该或不应该只使用一个这样的课程:
class Duck(object):
def speak(self):
print("quack")
class Cat(Animal):
def speak(self):
print("meow")
Run Code Online (Sandbox Code Playgroud) 如何将列表列表转换为字典列表?
更具体:我如何从这个:
[['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1', 'i1'], ['a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2', 'i2'], ['a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3', 'i3'], ['a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4', 'i4'], ['a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5', 'i5'], ['a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6', 'i6'], ['a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7', 'i7'], ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8', 'i8'], ['a9', 'b9', 'c9', 'd9', 'e9', 'f9', …Run Code Online (Sandbox Code Playgroud) 如果你可以使用指针迭代这样的数组:
for (int *iter = arr; iter != std::end(arr); ++iter) {
// code
}
Run Code Online (Sandbox Code Playgroud)
如何使用指针迭代多维数组(不使用auto)?
编辑:我假设这是一个int[][]如此{{3, 6, 8}, {2, 9, 3}, {4, 8, 2}}
我只是在编写一个程序,其中有一个继承自a的类tuple.但是,当我将参数作为列表传递时,它仍然有效.这是我的一些代码:
class Board(tuple):
def __init__(self, tup):
print(type(tup))
super().__init__()
board = Board([[Cell(False) for i in range(10)] for j in range(10)])
print(type(board))
Run Code Online (Sandbox Code Playgroud)
然后该程序输出:
<class 'list'>
<class '__main__.Board'>
Run Code Online (Sandbox Code Playgroud)
我的问题如下:为什么list当我说我会通过时,Python让我传递一个参数作为参数tuple?