-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Specify the minimum version of CMake required | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
# Project name and version | ||
project(ArrayProject VERSION 1.0) | ||
|
||
# Set C++ standard | ||
set(CMAKE_CXX_STANDARD 11) | ||
set(CMAKE_CXX_STANDARD_REQUIRED True) | ||
|
||
# Include directories | ||
include_directories(include) | ||
|
||
# Output executables to the 'bin' directory | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin) | ||
|
||
# Add the test executable | ||
add_executable(test_Array __test__/test_Array.cpp) | ||
|
||
# Link the Array library to the test executable | ||
target_link_libraries(test_Array) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 数组 | ||
|
||
![数组](./assets/array.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "../include/Array.h" | ||
#include <cassert> | ||
#include <iostream> | ||
|
||
void test_array() { | ||
Array<int, 0> arr_0; | ||
assert(arr_0.size() == 0); | ||
assert(arr_0.empty() == true); | ||
try{ | ||
arr_0.front(); | ||
} catch(const std::underflow_error &e){ | ||
std::cout << "front() throw an exception PASSED! " << e.what() << std::endl; // Expected | ||
} | ||
try{ | ||
arr_0.back(); | ||
} catch(const std::underflow_error &e){ | ||
std::cout << "back() throw an exception PASSED! " << e.what() << std::endl; // Expected | ||
} | ||
|
||
Array<int, 5> arr_5; | ||
assert(arr_5.size() == 5); | ||
assert(arr_5.empty() == false); | ||
try { | ||
assert(arr_5[0] == 0); | ||
assert(arr_5.at(4) == 0); | ||
arr_5.at(5); | ||
} catch (const std::out_of_range& e) { | ||
std::cout << "at() throw an exception PASSED! " << e.what() << std::endl; // Expected | ||
} | ||
for (int i = 0; i < 5; ++i) { | ||
arr_5[i] = i + 1; | ||
} | ||
assert(arr_5.front() == 1); | ||
assert(arr_5.back() == 5); | ||
assert(arr_5.at(2) == 3); | ||
assert(arr_5[3] == 4); | ||
arr_5[3] = 9; | ||
assert(arr_5[3] == 9); | ||
for(int i = 0; i < arr_5.size(); ++i){ | ||
std::cout << arr_5[i] << " "; | ||
} | ||
std::cout << std::endl; | ||
std::cout << "All tests passed!" << std::endl; | ||
} | ||
|
||
int main() { | ||
test_array(); | ||
return 0; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
|
||
# Create the build directory if it doesn't exist | ||
if [ ! -d "build" ]; then | ||
mkdir build | ||
fi | ||
|
||
# Run CMake in the build directory | ||
cd build | ||
cmake .. | ||
|
||
# Build the project using make | ||
make | ||
|
||
# Return to the project root directory | ||
cd .. | ||
|
||
# Inform the user where the executable can be found | ||
echo "Build complete. Executable located in the ./bin directory." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
# Remove build and bin directories | ||
rm -rf build | ||
rm -rf bin | ||
|
||
echo "Clean complete. Build and bin directories removed." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#ifndef ARRAY_H | ||
#define ARRAY_H | ||
|
||
#include <cstddef> | ||
#include <stdexcept> | ||
|
||
template <typename T, size_t N> | ||
class Array{ | ||
public: | ||
Array(); | ||
~Array() = default; | ||
public: | ||
// Capacity | ||
bool empty() const {return N == 0;} | ||
size_t size() const {return N;} | ||
// Element access | ||
T& front(); | ||
T& back(); | ||
T& at(const size_t index); | ||
T& operator[](const size_t index); | ||
private: | ||
T _data[N]; | ||
}; | ||
|
||
// Element access | ||
template <typename T, size_t N> | ||
T& Array<T, N>::front(){ | ||
if(N == 0){ | ||
throw std::underflow_error("front(): This Array is empty."); | ||
} | ||
return _data[0]; | ||
} | ||
|
||
template <typename T, size_t N> | ||
T& Array<T, N>::back(){ | ||
if(N == 0){ | ||
throw std::underflow_error("back(): This Array is empty."); | ||
} | ||
return _data[N - 1]; | ||
} | ||
|
||
template <typename T, size_t N> | ||
T& Array<T, N>::at(const size_t index){ | ||
if(index >= N){ | ||
throw std::out_of_range("at(): Index out of range."); | ||
} | ||
return _data[index]; | ||
} | ||
|
||
template <typename T, size_t N> | ||
T& Array<T, N>::operator[](const size_t index){ | ||
return _data[index]; | ||
} | ||
|
||
// Constructor | ||
template <typename T, size_t N> | ||
Array<T, N>::Array(){ | ||
for(size_t i = 0; i < N; ++i){ | ||
_data[i] = T(); | ||
} | ||
} | ||
|
||
#endif // ARRAY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters