本文整理汇总了C++中SpreadsheetCell::set方法的典型用法代码示例。如果您正苦于以下问题:C++ SpreadsheetCell::set方法的具体用法?C++ SpreadsheetCell::set怎么用?C++ SpreadsheetCell::set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpreadsheetCell
的用法示例。
在下文中一共展示了SpreadsheetCell::set方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: invalid_argument
SpreadsheetCell operator/(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)
{
if (rhs.mValue == 0)
throw invalid_argument("Divide by zero.");
SpreadsheetCell newCell;
newCell.set(lhs.mValue / rhs.mValue); // call set to update mValue and mString
return newCell;
}
示例2:
SpreadsheetCell operator*(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)
{
SpreadsheetCell newCell;
newCell.set(lhs.mValue * rhs.mValue); // call set to update mValue and mString
return newCell;
}
示例3: main
int main()
{
// On the stack (destroyed when out of scope)
SpreadsheetCell stackCell, stackCell2; // Omit parentheses for the default constructor
stackCell.set(6);
stackCell2.set("3.2");
cout << "stack cell 1: " << stackCell.getValue() << endl;
cout << "stack cell 2: " << stackCell2.getValue() << endl;
// Constructor on the stack
SpreadsheetCell stackCell3(4.2), stackCell4(9.2);
cout << "stack cell 3: " << stackCell3.getValue() << endl;
cout << "stack cell 4: " << stackCell4.getValue() << endl;
// On the heap
SpreadsheetCell* heapCell = new SpreadsheetCell();
heapCell->set(3.7);
cout << "heap cell 1: " << heapCell->getValue() << endl;
delete heapCell;
heapCell = nullptr;
// Constructor on the heap
auto heapCell2 = new SpreadsheetCell(44);
cout << "heap cell 2: " << heapCell2->getValue() << endl;
delete heapCell2;
heapCell2 = nullptr;
// Smart pointers for the heap
auto smartCell = make_unique<SpreadsheetCell>();
smartCell->set(3.14159);
cout << "smart cell 1: " << smartCell->getValue() << endl;
// Constructor with for pointer to the heap
auto smartCell2 = make_unique<SpreadsheetCell>(42);
cout << "smart cell 2: " << smartCell2->getValue() << endl;
// Manual heap delayed instantiation
SpreadsheetCell* heapCell3 = nullptr;
heapCell3 = new SpreadsheetCell(55);
cout << "heap cell 3: " << heapCell3->getValue() << endl;
delete heapCell3;
heapCell3 = nullptr;
// Array instantiation requires a default constructor (as to STL collections)
const int size = 3;
SpreadsheetCell cells[3];
SpreadsheetCell* moreCells = new SpreadsheetCell[10]; // HEAP!
// Stack arrays can use an initializer for non-default constructors
SpreadsheetCell stackCells[3] = {
SpreadsheetCell(3),
SpreadsheetCell(4),
SpreadsheetCell(5)
};
for (int i = 0; i < size; i++) {
cout << "arr cell " << i << ": " << cells[i].getValue() << endl;
cout << "heap arr cell " << i << ": " << moreCells[i].getValue() << endl;
cout << "stack arr cell " << i << ": " << stackCells[i].getValue() << endl;
}
delete[] moreCells; // CLEAN THE HEAP!
moreCells = nullptr;
// Copy
auto copyCell = SpreadsheetCell(stackCell);
cout << "copy cell: " << copyCell.getValue() << endl;
// Initializer-List Constructor
// EvenSequence p1 = {1.0, 2.0, 3.0, 4.0, 5.5, 7.7}; // or
EvenSequence p1 {1.0, 2.0, 3.0, 4.0, 5.5, 7.7};
p1.dump();
try {
EvenSequence p2 = {1.0, 2.0, 3.0};
} catch (const invalid_argument& err) {
cout << err.what() << endl;
}
// std initializer list
vector<string> myVec {"Hello", "World"};
for (auto value : myVec) {
cout << value << endl;
}
// Done
return 0;
}