Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
add41d9c
T
TinySTL
项目概览
OpenDocCN
/
TinySTL
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TinySTL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
add41d9c
编写于
9月 22, 2014
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完成vector容器
上级
51df860f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
111 addition
and
17 deletion
+111
-17
TinySTL/Vector.h
TinySTL/Vector.h
+90
-16
TinySTL/main.cpp
TinySTL/main.cpp
+21
-1
未找到文件。
TinySTL/Vector.h
浏览文件 @
add41d9c
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <array>
#include <algorithm>
#include <type_traits>
#include "Allocator.h"
...
...
@@ -11,7 +13,7 @@ namespace TinySTL{
namespace
{
template
<
class
T
>
class
viter
:
public
iterator
<
random_access_iterator
<
T
,
ptrdiff_t
>
,
T
>
{
class
viter
:
public
TinySTL
::
iterator
<
TinySTL
::
random_access_iterator_tag
,
T
>
{
private:
T
*
ptr_
;
private:
...
...
@@ -22,6 +24,8 @@ namespace TinySTL{
viter
(
const
viter
&
vit
);
viter
&
operator
=
(
const
viter
&
vit
);
operator
void
*
(){
return
ptr_
;
}
//change to the primitive pointer type
T
&
operator
*
(){
return
*
ptr_
;
}
T
*
operator
->
(){
return
&
(
operator
*
());
}
...
...
@@ -69,6 +73,7 @@ namespace TinySTL{
}
}
// end of anonymous namespace
//********* vector *************
template
<
class
T
,
class
Alloc
=
allocator
<
T
>
>
class
vector
{
private:
...
...
@@ -79,12 +84,12 @@ namespace TinySTL{
typedef
Alloc
dataAllocator
;
//Alloc dataAllocator;
public:
typedef
T
value_type
;
typedef
viter
<
T
>
iterator
;
typedef
iterator
pointer
;
typedef
T
&
reference
;
typedef
size_t
size_type
;
typedef
typename
iterator
::
difference_type
difference_type
;
typedef
T
value_type
;
typedef
viter
<
T
>
iterator
;
typedef
iterator
pointer
;
typedef
T
&
reference
;
typedef
size_t
size_type
;
typedef
typename
iterator
::
difference_type
difference_type
;
public:
//构造,复制,析构相关函数
vector
()
...
...
@@ -97,8 +102,7 @@ namespace TinySTL{
vector
(
vector
&&
v
);
vector
&
operator
=
(
const
vector
&
v
);
~
vector
(){
dataAllocator
::
destroy
(
start_
,
finish_
);
dataAllocator
::
deallocate
(
start_
,
endOfStorage_
-
start_
);
destroyAndDeallocateAll
();
}
//迭代器相关
...
...
@@ -129,26 +133,27 @@ namespace TinySTL{
std
::
swap
(
endOfStorage_
,
v
.
endOfStorage_
);
}
}
//TODO
void
push_back
(
const
value_type
&
value
);
void
pop_back
(){
--
finish_
;
dataAllocator
::
destroy
(
finish_
);
}
//TODO
iterator
insert
(
iterator
position
,
const
value_type
&
val
);
//TODO
void
insert
(
iterator
position
,
size_type
n
,
const
value_type
&
val
);
//TODO
void
insert
(
iterator
position
,
const
size_type
&
n
,
const
value_type
&
val
);
template
<
class
InputIterator
>
void
insert
(
iterator
position
,
InputIterator
first
,
InputIterator
last
);
//TODO
iterator
erase
(
iterator
position
);
iterator
erase
(
iterator
first
,
iterator
last
);
//容器的空间配置器相关
Alloc
get_allocator
(){
return
dataAllocator
;
}
private:
void
destroyAndDeallocateAll
(){
if
(
capacity
()
!=
0
){
dataAllocator
::
destroy
(
start_
,
finish_
);
dataAllocator
::
deallocate
(
start_
,
endOfStorage_
-
start_
);
}
}
void
allocateAndFillN
(
const
size_type
n
,
const
value_type
&
value
){
start_
=
dataAllocator
::
allocate
(
n
);
TinySTL
::
uninitialized_fill_n
(
start_
,
n
,
value
);
...
...
@@ -169,7 +174,15 @@ namespace TinySTL{
void
vector_aux
(
Integer
n
,
Integer
value
,
std
::
true_type
){
allocateAndFillN
(
n
,
value
);
}
};
template
<
class
InputIterator
>
void
insert_aux
(
iterator
position
,
InputIterator
first
,
InputIterator
last
,
std
::
false_type
);
template
<
class
Integer
>
void
insert_aux
(
iterator
position
,
Integer
n
,
Integer
value
,
std
::
true_type
);
template
<
class
InputIterator
>
void
reallocateAndCopy
(
iterator
position
,
InputIterator
first
,
InputIterator
last
);
void
reallocateAndFillN
(
iterator
position
,
const
size_type
&
n
,
const
value_type
&
val
);
};
// end of class vector
//***********************构造,复制,析构相关***********************
template
<
class
T
,
class
Alloc
>
vector
<
T
,
Alloc
>::
vector
(
const
size_type
n
){
...
...
@@ -221,6 +234,67 @@ namespace TinySTL{
}
return
viter
<
T
>
(
first
);
}
template
<
class
T
,
class
Alloc
>
template
<
class
InputIterator
>
void
vector
<
T
,
Alloc
>::
reallocateAndCopy
(
iterator
position
,
InputIterator
first
,
InputIterator
last
){
difference_type
oldCapacity
=
endOfStorage_
-
start_
;
oldCapacity
=
oldCapacity
?
oldCapacity
:
1
;
difference_type
newCapacity
=
oldCapacity
+
std
::
max
(
oldCapacity
,
last
-
first
);
T
*
newStart
=
dataAllocator
::
allocate
(
newCapacity
);
T
*
newEndOfStorage
=
newStart
+
newCapacity
;
T
*
newFinish
=
uninitialized_copy
(
begin
(),
position
,
newStart
);
newFinish
=
uninitialized_copy
(
first
,
last
,
newFinish
);
newFinish
=
uninitialized_copy
(
position
,
end
(),
newFinish
);
destroyAndDeallocateAll
();
start_
=
newStart
;
finish_
=
newFinish
;
endOfStorage_
=
newEndOfStorage
;
}
template
<
class
T
,
class
Alloc
>
template
<
class
InputIterator
>
void
vector
<
T
,
Alloc
>::
insert_aux
(
iterator
position
,
InputIterator
first
,
InputIterator
last
,
std
::
false_type
){
difference_type
locationLeft
=
endOfStorage_
-
finish_
;
// the size of left storage
difference_type
locationNeed
=
last
-
first
;
if
(
locationLeft
>=
locationNeed
){
auto
tempPtr
=
end
()
-
1
;
for
(;
tempPtr
-
position
>=
0
;
--
tempPtr
){
//move the [position, finish_) back
*
(
tempPtr
+
locationNeed
)
=
*
tempPtr
;
}
uninitialized_copy
(
first
,
last
,
position
);
finish_
+=
locationNeed
;
}
else
{
reallocateAndCopy
(
position
,
first
,
last
);
}
}
template
<
class
T
,
class
Alloc
>
template
<
class
Integer
>
void
vector
<
T
,
Alloc
>::
insert_aux
(
iterator
position
,
Integer
n
,
Integer
value
,
std
::
true_type
){
vector
<
value_type
>
v
(
n
,
value
);
insert
(
position
,
v
.
begin
(),
v
.
end
());
}
template
<
class
T
,
class
Alloc
>
template
<
class
InputIterator
>
void
vector
<
T
,
Alloc
>::
insert
(
iterator
position
,
InputIterator
first
,
InputIterator
last
){
insert_aux
(
position
,
first
,
last
,
typename
std
::
is_integral
<
InputIterator
>::
type
());
}
template
<
class
T
,
class
Alloc
>
void
vector
<
T
,
Alloc
>::
insert
(
iterator
position
,
const
size_type
&
n
,
const
value_type
&
val
){
insert_aux
(
position
,
n
,
val
,
typename
std
::
is_integral
<
value_type
>::
type
());
}
template
<
class
T
,
class
Alloc
>
typename
vector
<
T
,
Alloc
>::
iterator
vector
<
T
,
Alloc
>::
insert
(
iterator
position
,
const
value_type
&
val
){
insert
(
position
,
1
,
val
);
return
position
;
}
template
<
class
T
,
class
Alloc
>
void
vector
<
T
,
Alloc
>::
push_back
(
const
value_type
&
value
){
insert
(
end
(),
value
);
}
}
#endif
\ No newline at end of file
TinySTL/main.cpp
浏览文件 @
add41d9c
...
...
@@ -12,7 +12,27 @@
using
namespace
std
;
int
main
(){
TinySTL
::
vector
<
std
::
string
>
svec
(
10
,
"hello world"
);
int
array
[]
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
};
//TinySTL::vector<int> svec(array, array + 5);
//svec.erase(svec.begin() + 1, svec.begin() + 4);//1,5
TinySTL
::
vector
<
int
>
svec
;
cout
<<
svec
.
capacity
()
<<
endl
;
svec
.
push_back
(
1
);
cout
<<
svec
.
capacity
()
<<
endl
;
svec
.
push_back
(
2
);
cout
<<
svec
.
capacity
()
<<
endl
;
svec
.
push_back
(
3
);
cout
<<
svec
.
capacity
()
<<
endl
;
svec
.
push_back
(
4
);
cout
<<
svec
.
capacity
()
<<
endl
;
svec
.
push_back
(
5
);
cout
<<
svec
.
capacity
()
<<
endl
;
svec
.
push_back
(
6
);
cout
<<
svec
.
capacity
()
<<
endl
;
svec
.
push_back
(
7
);
cout
<<
svec
.
capacity
()
<<
endl
;
//svec.insert(svec.begin() + 0, array, array + 10);
//svec.insert(svec.begin()+0, 10, 99);
for
(
auto
s
:
svec
){
cout
<<
s
<<
endl
;
}
system
(
"pause"
);
return
0
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录