alinshans / mytinystl Goto Github PK
View Code? Open in Web Editor NEWAchieve a tiny STL in C++11
License: Other
Achieve a tiny STL in C++11
License: Other
In the insert function of the vector.h file, (line 469)
the else if
statement is missing a ++end_;
statement or end_ = new_end
; statement
哈希表使用开链法查找数据不够快,希望尽量用探测法去解决哈希冲突
两个其实都可以,各有各的好处,你把它分成两个类模版:cc_hash_table和gp_hash_table(对应开链法和探测发)
报了一堆错误,下面贴出了一部分。该如何解决??
Scanning dependencies of target stltest [ 50%] Building CXX object Test/CMakeFiles/stltest.dir/test.cpp.o In file included from /root/c++/MyTinySTL/Test/list_test.h:8, from /root/c++/MyTinySTL/Test/test.cpp:14: /root/c++/MyTinySTL/Test/../MyTinySTL/list.h: In instantiation of ‘void mystl::list<T>::remove_if(UnaryPredicate) [with UnaryPredicate = bool (*)(int); T = int]’: /root/c++/MyTinySTL/Test/list_test.h:67:3: required from here /root/c++/MyTinySTL/Test/../MyTinySTL/list.h:713:33: warning: implicitly-declared ‘mystl::list_iterator<int>& mystl::list_iterator<int>::operator=(const mystl::list_iterator<int>&)’ is deprecated [-Wdeprecated-copy] 713 | for (auto next = f; f != l; f = next) | ~~^~~~~~ /root/c++/MyTinySTL/Test/../MyTinySTL/list.h:115:3: note: because ‘mystl::list_iterator<int>’ has user-provided ‘mystl::list_iterator<T>::list_iterator(const mystl::list_iterator<T>&) [with T = int]’ 115 | list_iterator(const list_iterator& rhs) | ^~~~~~~~~~~~~
// 在 pos 位置就地构建元素
template
template <class ...Args>
typename deque::iterator deque::emplace(iterator pos, Args&& ...args)
{
if (pos.cur == begin_.cur)
{
emplace_front(mystl::forward(args)...);
return begin_;
}
else if (pos.cur == end_.cur)
{
emplace_back(mystl::forward(args)...);
// 有误,应该是 end_ - 1;
return end_;
}
return insert_aux(pos, mystl::forward(args)...);
}
Hi Alinshans,
Thanks for share such a good project. I have a question regarding to the RUN_ALL_TESTS.
I can't figure out how the test cases are created and registered. I just see a TestCase constructor takes one parameter, but the UnitTest GetInstance will use TestCase's default constructor, there is no parameter to initialize testcase_name. Also, I don't see where the RegisterTestCase get called.
I am looking forward to your answer. Thanks in advance.
Jimailster
如题,机制类似:
struct Guard {
ContainerType* p_container_;
~Guard() {
if (p_container_)
p_container_->rollback_operation();
}
};
在需要异常回滚的代码之前创建一个 Guard
对象:
Guard guard_{this};
在操作成功后将防护废弃掉:
guard_.p_container_ = nullptr;
测试 vector.insert 的时候,如果在尾部插入的话,是没问题的:
After v1.insert(v1.end(), 7) :
v1 : 0 1 2 3 4 6 6 7
但我再在头部插入元素时,会出现没有打印全的情况:
After v1.insert(v1.begin(), 7) :
v1 : 7 0 1 2 3 4 6 6 // 应该是 7 0 1 2 3 4 6 6 7
在代码(vector.h 407行):
if (end_ != cap_ && xpos == end_)
{
data_allocator::construct(mystl::address_of(*end_), mystl::forward<Args>(args)...);
++end_; //这里更新了,下面没有更新
}
else if (end_ != cap_)
{
auto new_end = end_;
data_allocator::construct(mystl::address_of(*end_), *(end_ - 1));
++new_end;
mystl::copy_backward(xpos, end_ - 1, end_);
*xpos = value_type(mystl::forward<Args>(args)...);
}
应该缺少一行 end_ = new_end;
MyTinySTL/MyTinySTL/basic_string.h
Line 1743 in 62ef9ef
Line 378 in 62ef9ef
不好意思,我算是小白中的小白了,直接下载代码之后用vs17打开sln,并尝试用release模式运行,结果编译器报错
错误 C2059 语法错误:“)” MyTinySTL d:\code\mytinystl-master\mytinystl-master\test\algorithm_test.h 722
按理来说不应该出现语法相关的报错才对,我应该使用vs15和c++11来学吗
template <class T>
void allocator<T>::deallocate(T* ptr)
{
if (ptr == nullptr)
return;
::operator delete(ptr);
}
template <class T>
void allocator<T>::deallocate(T* ptr, size_type /*size*/)
{
if (ptr == nullptr)
return;
::operator delete(ptr);
}
第二个函数为啥要注释size?本意是用来析构T数组的嘛,那为啥不用
operator delete[] (ptr)
?
本人菜鸟一只,希望得到解答
Line 235 in 8d88c6e
好像是少个头文件吧
在Vector的插入函数【特指 iterator insert(const_iterator pos, const value_type& value)这一重载】里中的一个判断分支 如果容量大于元素数量,且传入的迭代器不为end_时的代码我觉得可以简化一下
也就是这段:
else if (end_ != cap_)
{
auto new_end = end_;
data_allocator::construct(mystl::address_of(*end_), *(end_ - 1));
++new_end;
auto value_copy = value; // 避免元素因以下复制操作而被改变
mystl::copy_backward(xpos, end_ - 1, end_);
*xpos = mystl::move(value_copy);
end_ = new_end;
}
我觉得,vector储存元素的内存地址既然是连续的,就没有必要用allocator专门来给end_重新构造一次来储存最后一个元素了吧。
可以直接利用copy_backward来实现元素复制。
我的实现:
else if (end_ != cap_)
{
auto value_copy = value;
++end_;
std::copy_backward(x_pos, end_ - 1, end_);//我暂时仍在使用stl提供的算法
*x_pos = std::move(value_copy);
}
我是一个C++新手,理解可能有误,如果有错误,请指正!
MyTinySTL/MyTinySTL/uninitialized.h
Line 194 in 8d88c6e
explicit vector(size_type n)
{ fill_init(n, value_type()); }
为什么value_type 后面要添加() , 我猜测这个是函数吧。它的原型是 mystl::allocator<T>::value_type()
吗? 可我在 allocator 中没有找到对应函数的实现呀
../MyTinySTL/vector.h: In member function ‘void mystl::vector::reverse()’:
../MyTinySTL/vector.h:276:31: error: ‘reverse’ is not a member of ‘mystl’
276 | void reverse() { mystl::reverse(begin(), end()); }
MyTinySTL/MyTinySTL/basic_string.h
Line 2024 in 62ef9ef
你能用 libc++ 的 testsuite 跑一遍吗?
has_iterator_cat这个结构体的定义中,有一行是 template <class U> static two test(...);
,这一句的(...)是什么意思呀
对于已经定义了下面的copy构造函数:
// implicit constructiable for other pair
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, const Other1&>::value &&
std::is_constructible<Ty2, const Other2&>::value &&
std::is_convertible<const Other1&, Ty1>::value &&
std::is_convertible<const Other2&, Ty2>::value, int>::type = 0>
constexpr pair(const pair<Other1, Other2>& other)
: first(other.first),
second(other.second)
{
}
为什么还要定义explicit的copy构造函数:
// explicit constructiable for other pair
template <class Other1, class Other2,
typename std::enable_if<
std::is_constructible<Ty1, const Other1&>::value &&
std::is_constructible<Ty2, const Other2&>::value &&
(!std::is_convertible<const Other1&, Ty1>::value ||
!std::is_convertible<const Other2&, Ty2>::value), int>::type = 0>
explicit constexpr pair(const pair<Other1, Other2>& other)
: first(other.first),
second(other.second)
{
}
我理解模板参数的含义,就是不知道为什么这样定义,或者可以指出在什么情况下才会调用下面的 explicit的copy构造函数吗。谁能帮忙解答下?谢谢。
如题: C++11 拥有 forward_list
容器。
大家好,我在 windows10
上使用 vs2015
编译测试MyTinySTL
的时候,编译没有通过。编译器给出了115条的语法错误,但是我在回看了这些提示错误的代码的时候,没有发现我认为错误的语法。求大家指点迷津,编译器给出的语法错误信息的部分信息如下:
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(11): error C2504: 'input_iterator_tag': base class undefined
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(18): error C3646: 'iterator_category': unknown override specifier
1> c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(23): note: see reference to class template instantiation 'mystl::input_iterator<T,Distance>' being compiled
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(18): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(78): error C2653: 'Iterator': is not a class or namespace name
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(78): error C3646: 'iterator_category': unknown override specifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(78): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(79): error C2653: 'Iterator': is not a class or namespace name
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(79): error C3646: 'value_type': unknown override specifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(79): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(80): error C2653: 'Iterator': is not a class or namespace name
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(80): error C3646: 'difference_type': unknown override specifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(80): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(81): error C2653: 'Iterator': is not a class or namespace name
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(81): error C3646: 'pointer': unknown override specifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(81): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(82): error C2653: 'Iterator': is not a class or namespace name
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(82): error C3646: 'reference': unknown override specifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(82): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(87): error C2989: 'mystl::iterator_traits': class template has already been declared as a non-class template
1> c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(77): note: see declaration of 'mystl::iterator_traits'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(87): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(87): error C2059: syntax error: '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(96): error C2989: 'mystl::iterator_traits': class template has already been declared as a non-class template
1> c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(87): note: see declaration of 'mystl::iterator_traits'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(96): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(96): error C2059: syntax error: '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(106): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(106): error C2059: syntax error: '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(115): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(115): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(121): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(121): error C2059: syntax error: '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(128): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(128): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(133): warning C4091: 'inline ': ignored on left of 'mystl::iterator_traits' when no variable is declared
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(133): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(133): error C2143: syntax error: missing ';' before '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(133): error C2059: syntax error: '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(134): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(134): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(144): warning C4091: 'inline ': ignored on left of 'mystl::iterator_traits' when no variable is declared
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(144): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(144): error C2143: syntax error: missing ';' before '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(144): error C2059: syntax error: '<'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(145): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(145): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(150): error C2182: 'advance': illegal use of type 'void'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(150): error C2433: 'advance': 'inline' not permitted on data declarations
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(150): error C2448: 'mystl::advance': function-style initializer appears to be a function definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(155): error C2182: '__advance': illegal use of type 'void'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(155): error C2433: '__advance': 'inline' not permitted on data declarations
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(155): error C2448: 'mystl::__advance': function-style initializer appears to be a function definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(160): error C2182: '__advance': illegal use of type 'void'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(160): error C2433: '__advance': 'inline' not permitted on data declarations
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(160): error C2374: 'mystl::__advance': redefinition; multiple initialization
1> c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(155): note: see declaration of 'mystl::__advance'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(160): error C2448: 'mystl::__advance': function-style initializer appears to be a function definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(168): error C2182: '__advance': illegal use of type 'void'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(168): error C2433: '__advance': 'inline' not permitted on data declarations
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(168): error C2374: 'mystl::__advance': redefinition; multiple initialization
1> c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(155): note: see declaration of 'mystl::__advance'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h(168): error C2448: 'mystl::__advance': function-style initializer appears to be a function definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\iterator.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(39): error C2143: syntax error: missing ';' before 'identifier'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(39): error C2143: syntax error: missing ';' before '->'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(39): error C2270: '->': modifiers not allowed on nonmember functions
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(39): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(39): error C2801: 'mystl::operator ->' must be a non-static member
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(40): error C3861: '*': identifier not found
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(44): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(44): error C2143: syntax error: missing ';' before '&'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(44): error C2059: syntax error: '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(44): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(44): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(48): error C2143: syntax error: missing ';' before '++'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(48): error C2803: 'operator ++' must have at least one formal parameter of class type
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(49): error C2065: 'self': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(49): error C2146: syntax error: missing ';' before identifier 'tmp'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(49): error C2065: 'tmp': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(49): error C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(50): error C2065: 'current': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(51): error C2065: 'tmp': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(54): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(54): error C2143: syntax error: missing ';' before '&'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(54): error C2059: syntax error: '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(54): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(54): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(58): error C2143: syntax error: missing ';' before '--'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(58): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(58): error C2803: 'operator --' must have at least one formal parameter of class type
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(59): error C2065: 'self': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(59): error C2146: syntax error: missing ';' before identifier 'tmp'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(59): error C2065: 'tmp': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(59): error C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(60): error C2065: 'current': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(61): error C2065: 'tmp': undeclared identifier
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(64): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(64): error C2143: syntax error: missing ';' before '&'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(64): error C2473: 'operator +=': looks like a function definition, but there is no parameter list.
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(64): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(64): error C2448: 'mystl::+=': function-style initializer appears to be a function definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(68): error C2143: syntax error: missing ';' before '+'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(68): error C2473: 'operator +': looks like a function definition, but there is no parameter list.
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(68): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(68): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(68): error C2059: syntax error: 'const'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(68): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(68): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(71): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(71): error C2143: syntax error: missing ';' before '&'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(71): error C2473: 'operator -=': looks like a function definition, but there is no parameter list.
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(71): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(71): error C2448: 'mystl::-=': function-style initializer appears to be a function definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): error C2143: syntax error: missing ';' before '-'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): error C2473: 'operator -': looks like a function definition, but there is no parameter list.
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): error C2988: unrecognizable template declaration/definition
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): error C2059: syntax error: 'const'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): error C2143: syntax error: missing ';' before '{'
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\jiwaa\downloads\mytinystl-1.1.0\mytinystl-1.1.0\mytinystl\reverse_iterator.h(75): fatal error C1003: error count exceeds 100;
MyTinySTL/vector.h中的range_init
函数定义时(598-608行),计算first
和last
的距离用了last-first
(606行),但是这仅限于迭代器是random_access_iterator才可以,应该用distance(first, last)
计算
// range_init 函数
template <class T>
template <class Iter>
void vector<T>::
range_init(Iter first, Iter last)
{
const size_type init_size = mystl::max(static_cast<size_type>(last - first),
static_cast<size_type>(16));
init_space(static_cast<size_type>(last - first), init_size);
mystl::uninitialized_copy(first, last, begin_);
}
在macOS10.15.7,xocde11.6(11E708),clang 11.0.3中vector.h中的276行的void reverse() { mystl::reverse(begin(), end()); }必须注释掉,否则会报命名空间错误的提示。
感觉上这样会让rb_tree把自己当作一个map,选择使用错误的rb_tree_value_traits_imp
Line 727 in 8d88c6e
data_allocator::destory(newStart, newFinish); data_allocator::deallocate(newStart, newCapacity);
\0
、 L\0
等)的设置是惰性的。这与标准要求不合,并且可能有并发调用 const
限定函数导致数据竞争的问题;capacity()
的返回值应该是当前分配的存储中能放下的字符串元素数,元素不含末尾的空终止符(此处与 Jonathan Wakely 确认过)。所以目前实现的值多了 1
。rhs.size() <= this->capacity()
时不应分配新存储。operator+
可以在一些情况不分配新存储。我看代码里面的RegisterTestCase(TestCase* testcase);函数才是将案例放进容器里,但test.cpp的main函数中没有看到这句话呢,我是错过了哪里,求大佬讲解一下
c++新手,primer看完了一半,看了一两天这个代码了,还没搞懂,里面用了好多的宏定义,这个看起来好费力,第一次见这么多的宏定义和头文件保护。想问下大家都是多久吃透了这个项目?
iterator.h里面重载运算符是不是有问题呀
template <class Iterator>
bool operator>(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return rhs < lhs; // 这个难道不应该是 !(rhs < lhs); 吗
}
问一下//原空间较小则新建一个vector,然后复制里面的内容到新空间。但是为什么原空间不要在此时释放呢?是要等到程序运行结束,调用析构函数的时候释放吗
为什么deallocate传入size和没传size是一样的操作呢?
::operator delete会将size个object全部释放吗?
https://github.com/Alinshans/MyTinySTL/blob/master/MyTinySTL/allocator.h#L67
I work on MSVC's C++ Standard Library implementation, and we regularly build open-source projects like MyTinySTL in order to prevent compiler and library bugs that would affect such projects. This also allows us to provide advance notice of source-breaking changes.
After merging microsoft/STL#2998 , we found compiler errors when building MyTinySTL. This is ultimately caused by:
Lines 515 to 521 in 3441980
These macros are _Leading_underscore_capital
. Such identifiers are reserved by the C++ Standard for use by the compiler and standard library. One of the headers that the C++ Standard Library drags in, <mmintrin.h>
, contains intrinsic declarations like
__m64 _mm_set1_pi16(short _S);
After preprocessing, this becomes __m64 _mm_set1_pi16(short / 5);
which can't compile. (The _Leading_underscore_capital
rule exists to prevent the standard library from conflicting with other code like this.)
This has always been a conformance issue, but it appears that we're seeing it now because microsoft/STL#2998 causes <mmintrin.h>
to be included later than it previously was, after test.h
has defined _S
.
To avoid this problem, all of these these test macros should be renamed to avoid the reserved patterns specified by N4910 5.10 [lex.name]/3.
你好,想问一下为什么反向迭代器里面的大于小于运算符是反的啊?
下面的小于运算符为什么要返回右值小于左值?不是应该返回左值小于右值吗?
template <class Iterator>
bool operator<(const reverse_iterator<Iterator>& lhs,
const reverse_iterator<Iterator>& rhs)
{
return rhs.base() < lhs.base();
}
原函数为
// reinsert 函数
template
void vector::reinsert(size_type size)
{
auto new_begin = data_allocator::allocate(size);
try
{
mystl::uninitialized_move(begin_, end_, new_begin);
}
catch (...)
{
data_allocator::deallocate(new_begin, size);
throw;
}
begin_ = new_begin;
end_ = begin_ + size;
cap_ = begin_ + size;
}
应该在move后归还原begin_开辟的内存。即:
// reinsert 函数
template
void vector::reinsert(size_type size)
{
auto new_begin = data_allocator::allocate(size);
try
{
mystl::uninitialized_move(begin_, end_, new_begin);
}
catch (...)
{
data_allocator::deallocate(new_begin, size);
throw;
}
data_allocator::deallocate(begin_, cap_ - begin_);//我觉得这应该不上这个语句,归还内存。
begin_ = new_begin;
end_ = begin_ + size;
cap_ = begin_ + size;
}
通过 initializer_list 初始化的时候,为什么不把参数分为 const std::initializer_list<value_type> &ilist
和 std::initializer_list<value_type> &&ilist
两种拷贝构造参数呢?
而是使用下面的这种方式,这是有什么顾虑吗
vector(std::initializer_list<value_type> ilist)
{
range_init(ilist.begin(), ilist.end());
}
MyTinySTL/MyTinySTL/basic_string.h
Line 94 in 62ef9ef
X::copy(s,p,n)
Requires: p
not in [s,s+n)
.
Returns: s.
for each i
in [0,n)
, performs X::assign(s[i],p[i])
.
MyTinySTL/MyTinySTL/basic_string.h
Line 2024 in 62ef9ef
return lhs.size() == rhs.size() && lhs.compare(rhs) == 0;
rt
friend bool operator==(const set& lhs, const set& rhs) { return lhs.tree_ == rhs.tree_; } friend bool operator< (const set& lhs, const set& rhs) { return lhs.tree_ < rhs.tree_; }
set.h第199行和200行
已经在类内定义了比较操作符,在类外又定义了一次,这样不会出现重定义的问题吗
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.