我正在尝试为数据结构类做作业。问题是 1) 使用定义的 UNIT_TEST 进行编译,然后使用 -d yes 运行以查看所需时间(以秒为单位)。在提交框中报告该时间以及您在测试选择排序时发现的时间。哪个更快?
我无法理解,我做错了什么。我收到此错误:
g++ -o bin/Debug/algAnalysis obj/Debug/main.o
Undefined symbols for architecture x86_64:
"Catch::NameAndTags::NameAndTags(Catch::StringRef const&, Catch::StringRef const&)", referenced from:
___cxx_global_var_init in main.o
"Catch::StringMaker<int, void>::convert(int)", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > Catch::Detail::stringify<int>(int const&) in main.o
"Catch::makeTestInvoker(void (*)())", referenced from:
___cxx_global_var_init in main.o
"Catch::AssertionHandler::handleExpr(Catch::ITransientExpression const&)", referenced from:
____C_A_T_C_H____T_E_S_T____0() in main.o
"Catch::AssertionHandler::handleUnexpectedInflightException()", referenced from:
____C_A_T_C_H____T_E_S_T____0() in main.o
"Catch::AssertionHandler::complete()", referenced from:
____C_A_T_C_H____T_E_S_T____0() in main.o
"Catch::AssertionHandler::AssertionHandler(Catch::StringRef, Catch::SourceLineInfo const&, Catch::StringRef, Catch::ResultDisposition::Flags)", referenced from:
____C_A_T_C_H____T_E_S_T____0() in main.o
"Catch::ITransientExpression::~ITransientExpression()", referenced from:
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > > >::MatchExpr(std::__1::vector<int, std::__1::allocator<int> > const&, Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > > const&, Catch::StringRef) in main.o
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > > >::~MatchExpr() in main.o
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Vector::EqualsMatcher<int> >::MatchExpr(std::__1::vector<int, std::__1::allocator<int> > const&, Catch::Matchers::Vector::EqualsMatcher<int> const&, Catch::StringRef) in main.o
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Vector::EqualsMatcher<int> >::~MatchExpr() in main.o
"Catch::ReusableStringStream::ReusableStringStream()", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > Catch::Detail::rangeToString<std::__1::__wrap_iter<int const*> >(std::__1::__wrap_iter<int const*>, std::__1::__wrap_iter<int const*>) in main.o
"Catch::ReusableStringStream::~ReusableStringStream()", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > Catch::Detail::rangeToString<std::__1::__wrap_iter<int const*> >(std::__1::__wrap_iter<int const*>, std::__1::__wrap_iter<int const*>) in main.o
"Catch::Detail::unprintableString", referenced from:
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > > >::streamReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const in main.o
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Vector::EqualsMatcher<int> >::streamReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const in main.o
"Catch::AutoReg::AutoReg(Catch::ITestInvoker*, Catch::SourceLineInfo const&, Catch::StringRef const&, Catch::NameAndTags const&)", referenced from:
___cxx_global_var_init in main.o
"Catch::AutoReg::~AutoReg()", referenced from:
___cxx_global_var_init in main.o
"Catch::Matchers::Impl::MatcherUntypedBase::~MatcherUntypedBase()", referenced from:
Catch::Matchers::Impl::MatcherBase<std::__1::vector<int, std::__1::allocator<int> > >::~MatcherBase() in main.o
"Catch::StringRef::StringRef(char const*)", referenced from:
____C_A_T_C_H____T_E_S_T____0() in main.o
___cxx_global_var_init in main.o
(maybe you meant: __ZN5Catch9StringRefC1EPKcm)
"Catch::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Catch::StringRef const&)", referenced from:
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > > >::streamReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const in main.o
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Vector::EqualsMatcher<int> >::streamReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const in main.o
"Stack::pop(int&)", referenced from:
SortClass<int>::sort(int*, int, int) in main.o
"Stack::push(int)", referenced from:
SortClass<int>::sort(int*, int, int) in main.o
"Stack::Stack(int)", referenced from:
SortClass<int>::sort(int*, int, int) in main.o
"Stack::~Stack()", referenced from:
SortClass<int>::sort(int*, int, int) in main.o
"Catch::ReusableStringStream::str() const", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > Catch::Detail::rangeToString<std::__1::__wrap_iter<int const*> >(std::__1::__wrap_iter<int const*>, std::__1::__wrap_iter<int const*>) in main.o
"Catch::Matchers::Impl::MatcherUntypedBase::toString() const", referenced from:
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > > >::streamReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const in main.o
Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > >::describe() const in main.o
Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Vector::EqualsMatcher<int> >::streamReconstructedExpression(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const in main.o
"typeinfo for Catch::ITransientExpression", referenced from:
typeinfo for Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Impl::MatchNotOf<std::__1::vector<int, std::__1::allocator<int> > > > in main.o
typeinfo for Catch::MatchExpr<std::__1::vector<int, std::__1::allocator<int> >, Catch::Matchers::Vector::EqualsMatcher<int> > in main.o
"typeinfo for Catch::Matchers::Impl::MatcherUntypedBase", referenced from:
typeinfo for Catch::Matchers::Impl::MatcherBase<std::__1::vector<int, std::__1::allocator<int> > > in main.o
"vtable for Catch::ITransientExpression", referenced from:
Catch::ITransientExpression::ITransientExpression(bool, bool) in main.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for Catch::Matchers::Impl::MatcherUntypedBase", referenced from:
Catch::Matchers::Impl::MatcherUntypedBase::MatcherUntypedBase(Catch::Matchers::Impl::MatcherUntypedBase const&) in main.o
Catch::Matchers::Impl::MatcherUntypedBase::MatcherUntypedBase() in main.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Process terminated with status 1 (0 minute(s), 0 second(s))
到目前为止,这是我的代码:
#define CATCH_CONFIG_MAI
#define UNIT_TEST
#include <iostream>
#include <fstream>
#include <vector>
#ifdef UNIT_TEST
#include "catch.hpp"
#endif // UNIT_TEST
#include "stack.h"
using namespace std;
template <class T>
class SortClass
{
private:
void swap(T &a, T&b);
int split(T a[], int first, int last);
public:
void sort(T a[], int first, int last);
};
template <typename T>
int SortClass <T>::split (T arr[], int first, int last)
{
T x= arr[last];
int i= (first-1);
for (int j=first; j <= last-1; j++)
{
if (arr[j]<= x)
{
i++;
swap (arr[i], arr[j]);
}
}
swap (arr[i+1], arr[last]);
return (i+1);
}
template <class T>
void SortClass <T>::sort(T a[], int first, int last)
{
Stack stack;
stack.push (first);
stack.push (last);
while (not stack.isEmpty())
{
stack.pop(last);
stack.pop(first);
int s= split (a, first, last);
if (s-1>first )
{
stack.push(first);
stack.push(s-1);
}
if (s+1<last)
{
stack.push(s+1);
stack.push(last);
}
}
}
template <class T>
void SortClass <T>:: swap(T &a, T &b)
{
T temp=a;
a=b;
b=temp;
}
#ifndef UNIT_TEST
int main ()
{
const int N=10;
int x1[N]={1,3,5,6,4,2,8,9,7};
for (auto e: x1) std:: cout << e << " ";
std:: cout << std:: endl;
SortClass <int> s; s.sort(x1,0,N-1);
for (auto e: x1) std:: cout << e << " ";
std:: cout << std:: endl;
}
#endif
#ifdef UNIT_TEST
using Catch::Matchers::Equals;
TEST_CASE("A fast sort")
{
const int N = 6;
int x1[N] = { 1, 3, 5, 6, 4, 2 };
int x2[N] = { 1, 2, 3, 4, 5, 6 };
vector<int> v1(x1, x1 + N);
vector<int> v2(x2, x2 + N);
REQUIRE_THAT(v1, not Equals(v2));
SortClass<int> s;
s.sort(x1, 0, N - 1);
vector<int> v0(x1, x1 + N);
REQUIRE_THAT(v0, Equals(v2));
const int M = 100000;
int x3[M]; int x4[M];
ifstream ifs1("numbers");
for (auto &e : x3)
ifs1 >> e;
ifstream ifs2("sorted");
for (auto &e : x4)
ifs2 >> e;
s.sort(x3, 0, M - 1);
vector<int> v3(x3, x3 + M);
vector<int> v4(x4, x4 + M);
REQUIRE_THAT(v3, Equals(v4));
}
#endif