# 如何使用向量实现BubblesortHow do I implement bubblesort using a vector

#include <iostream>
#include "UList.h"
#include "sortBS.h"
#include <vector>

using namespace std;

int main(){
UList<size_t> list1(20), list2;

cout<<list1.size()<<endl;
cout<<list2.size()<<endl;
list1.insert(3);
list1.insert(5);
list1.insert(4);
list1.erase(4);
cout<<list1.find(3)<<endl;
cout<<list1.find(4)<<endl;
cout<<list1.empty()<<endl;
cout<<list2.empty()<<endl;
sort(list1);
cout<<list1<<endl;
//sort
//<<

}

UList.h

#ifndef UList__H
#define UList__H
#include <iostream>
#include <vector>
#include "sortBS.h"
template <class T>
class UList{
public:
UList(size_t=10);
void insert(const T&);
bool erase(const T&);
bool find(const T&);
size_t size() const;
bool empty() const;
friend void sort (UList<T>&);
friend std::ostream& operator << (std::ostream&, const UList<T>&);

protected:
std::vector<T> items;
};

template <class T>
UList<T>::UList(size_t size){
items.resize(size);
}

template <class T>
void UList<T>::insert(const T& element){
items.insert(items.begin(), element);
}

template <class T>
bool UList<T>::erase(const T& element){
for(size_t index=0; index<items.size(); index++){
if(element==items.at(index))
items.erase(items.begin()+index);
return true;
}
return false;
}

template <class T>
bool UList<T>::find(const T& element){
bool found=false;
size_t index=0;
while(index<items.size()){
if(items.at(index)==element)
found=true;
index++;
}
return found;
}

template <class T>
size_t UList<T>::size() const{
return items.size();
}

template <class T>
bool UList<T>::empty() const{
return items.empty();
}

template<class T>
std::ostream& operator << (std::ostream& out, const UList<T>& List){
if(List.items.empty())
out<<"list is empty."<<std::endl;
else{
for(size_t index=0;index<List.items.size();index++){
out<<List.items.at(index);
if(index<List.items.size()-1)
out<<" ";
}
}
return out;
}

template<class T>
void sort(UList<T>& List){
sortBS(List);
}

#endif

sortBS.h

#ifndef sortBS__H
#define sortBS__H
#include <iostream>
#include <vector>
#include "UList.h"

template <class T>
void sortBS(UList<T> List){
for(size_t iteration=1;iteration<items.size();iteration++){
for(size_t index=0;index<items.size()-iteration;index++){
if(items.at(index)>items.at(index+1)){
T temp = items.at(index);
items.at(index) = items.at(index+1);
items.at(index+1) = temp;
}
}
}
}

#endif // sortBS__H

||=== Build: Debug in Program04 (compiler: GNU GCC Compiler) ===|
G:\Program04\sortBS.h|8|error: variable or field 'sortBS' declared void|
G:\Program04\sortBS.h|8|error: 'UList' was not declared in this scope|
G:\Program04\sortBS.h|8|error: expected primary-expression before '>' token|
G:\Program04\sortBS.h|8|error: 'List' was not declared in this scope|
G:\Program04\UList.h|15|warning: friend declaration 'void sort(UList<T>&)' declares a                 non-template function [-Wnon-template-friend]|
G:\Program04\UList.h|15|note: (if this is not what you intended, make sure the    function     template has already been declared and add <> after the function name here) |
G:\Program04\UList.h|16|warning: friend declaration 'std::ostream&    operator<<(std::ostream&, const UList<T>&)' declares a non-template function [-Wnon-    template-friend]|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

UList<T>
#items: vector<T>
+UList(size_t=10)
+insert(const T&): void
+erase(const T&): bool
+find(const T&) const: bool
+size() const: size_t
+empty() const: bool
+friend operator << (ostream&, const UList<T>&): ostream&
+friend sort (UList<T>&): void
Submit sortBS.h and UList.h prior to the due date/time using the following form (I will        be using my own driver file for grading purposes):

## 2 个回复2

### ===============>>#1 票数：0

• 不要在UList.h中包含sortBS.h。 UList不需要sortBS存在（没有它就可以正常工作），因此不应这样包含它。 另外，由于在定义实际UList之前已包含该文件，因此会出现错误，提示sortBS不知道UList是什么。

• sortBS声明了一个名为List的变量，然后不使用它，并尝试使用一个不存在的名为“ items”的变量。 重命名其中一个以匹配另一个。

• sortBS的最后一部分可以简化为：

如果（items [index]> items [index + 1]）std :: swap（items [index]，items [index + 1]）;

### ===============>>#2 票数：0

template< typename C >
friend void sort (UList<C>&);
template< typename C>
friend std::ostream& operator << (std::ostream&, const UList<C>&);

template< typename T>
void sort(UList<T>& List){
vector<T> items(List.items);
for(size_t iteration=1;iteration<items.size();iteration++){
for(size_t index=0;index<items.size()-iteration;index++){
if(items.at(index)>items.at(index+1)){
T temp = items.at(index);
items.at(index) = items.at(index+1);
items.at(index+1) = temp;
}
}
}
}

template <class T>
class UList{
public:
UList(size_t=10);
void insert(const T&);
bool erase(const T&);
bool find(const T&);
size_t size() const;
bool empty() const;
friend void sort (UList<T>& List)
{
vector<T> items(List.items);
for(size_t iteration=1;iteration<items.size();iteration++){
for(size_t index=0;index<items.size()-iteration;index++){
if(items.at(index)>items.at(index+1)){
T temp = items.at(index);
items.at(index) = items.at(index+1);
items.at(index+1) = temp;
}
}
}
}
template< typename C>
friend std::ostream& operator << (std::ostream&, const UList<C>&);

protected:
std::vector<T> items;
};