Comments (4)
对于字符串move函数定义如下
// 字符串移动(复制),允许复制的两个地址段有重复,具体操作是更具源地址和目的地址关系选择从前后复制还是从后向前复制 static char_type* move(char_type* dst, const char_type* src, size_t n) { char_type* r = dst; if (dst < src) { for (; n != 0; --n, ++dst, ++src) *dst = *src; } else if (src < dst) { dst += n; src += n; for (; n != 0; --n) *--dst = *--src; } return r; }最后一个参数n的类型是size_t类型,这里使用count作为移动字符的数目应该没有问题
移动字符的数目就有问题,应该是先把pos到end()这一段往后移动count,再把[first, last)拷贝进来,所以移动数目是end()-pos,对应dst是r+count,src是r。
from mytinystl.
对于字符串move函数定义如下
// 字符串移动(复制),允许复制的两个地址段有重复,具体操作是更具源地址和目的地址关系选择从前后复制还是从后向前复制
static char_type* move(char_type* dst, const char_type* src, size_t n)
{
char_type* r = dst;
if (dst < src)
{
for (; n != 0; --n, ++dst, ++src)
*dst = *src;
}
else if (src < dst)
{
dst += n;
src += n;
for (; n != 0; --n)
*--dst = *--src;
}
return r;
}
最后一个参数n的类型是size_t类型,这里使用count作为移动字符的数目应该没有问题
from mytinystl.
我看了一下,确实如此,如果move第三个参数传递count,可能会导致某些值被覆盖而不是被移动,这逻辑确实有问题,还是等作者回复吧
from mytinystl.
是不是应该先将first到last之间的元素拷贝下来,在移动,因为移动可能改变了first和last指向的元素位置或者basic_string重新分配了内存
from mytinystl.
Related Issues (20)
- 求问,项目clone不到我的ubuntu去
- 关于容器中的allocator_type get_allocator()函数 HOT 1
- 关于unorderset_set的比较操作符 HOT 2
- vs2022 C++14 运行报错 HOT 8
- address_of不能获取对象的真实地址 HOT 5
- 请问在对this指针操作时,为什么使用&*this,而不是直接返回this
- basic_string.h中erase删除 [first, last) 的元素的问题 HOT 1
- hashtable构造函数可能有问题 HOT 2
- hashtable的insert_unique_use_hint可能有问题 HOT 1
- 关于测试mystl::vector<std::string> v3找不到可匹配的destroy的问题 HOT 1
- functional.h 类模板 `identity` 实现模板参数错误? HOT 2
- 请教下作者实现的STL相比标准库的STL整体性能略优,可能是哪些方面导致的 HOT 5
- 关于deque容器析构函数中可能会导致内存泄漏的问题 HOT 3
- vector& operator=(const vector& rhs);rhs的size大于当前对象的size并且小于等于当前的capacity时,会造成内存泄漏 HOT 1
- 你这里面的alloc.h 里面 有很大错误, 不建议阅读整个项目了. HOT 1
- uninitialized_fill_n 的模板偏特化疑问,MyTinySTL的行为与标准库不同 HOT 3
- 运行错误
- 请教一下作者,你在栈里分配千万数量级的内存,比如push_back那里,栈不会溢出吗 HOT 1
- 编译报错
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mytinystl.