在C#和Java中,您可以输入任何类型的数据-int,string,float,long-,而不会排除其他类型。
所以我想知道在C ++中有什么方法可以做到这一点吗?
由于与C#或Java不同,Object或Obj无法正常工作,坦率地说,我已经搜索了一段时间,但尚未得出结论。
我要使用的Function \ Method是从整数,字符串等中获取任何输入...或\和用户输入,然后将其打印回控制台。
代码是:
static void P(Object x) { cout << x;}
答案 0 :(得分:4)
使用模板:
def my_det(X):
'''
Parameters
----------
X : array_like
Returns
-------
det : float
Determinant of `a`.
Plan
----
Iterate through columns, create nested loop to iterate through all elements below
the main diagonal, assign a scaler according to the value, iterate through all
elements in the row to change them according to the scaler. Finally, compute
determinant.
'''
dimensionX, dimensionY = X.shape
if dimensionX == dimensionY:
matrixCopy = X.copy()
for i in range(dimensionX): # make only the diagonal non-zero
for getZero in range(i + 1, dimensionX): # make all elements below i-th zero(i.e. getZero elment = 0)
if matrixCopy[i, i] == 0: # cheating to create very small value
matrixCopy[i, i] == 1.0e-18
rowScaler = matrixCopy[getZero, i] / matrixCopy[i, i] # to change the rows accordingly
print(rowScaler)
for j in range(dimensionX): # change every element in the row
matrixCopy[getZero, j] = float(matrixCopy[getZero, j]) - float(rowScaler * matrixCopy[i, j])
print(matrixCopy[getZero, j])
print(matrixCopy)
det = 1.0
for detNum in range(dimensionX): # TADAAAAAAM
det *= matrixCopy[detNum, detNum]
return det
else:
print('ValueError: wrong dimensions')
# matrix = np.random.randint(50, size=(3, 3))
matrix = np.array([[39, 24, 16], [24, 45, 47], [2, 7, 28]])
# matrix = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
print(matrix)
print(my_det(matrix))
用法:
template <typename T>
void Print(const T& x)
{
std::cout << x;
}