版本比较
关于stl中rope的实用讲解
正文
增加 166 行删除 166 行
123456742 行未改动50515253545556575859606 行未改动6768697071727374755 行未改动818283848586878889909192937 行未改动10110210310410510610710810911011111211311411511611711811912012112212310 行未改动13413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316 行未改动1801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402413 行未改动24524624724824925025125225311 行未改动26526626726826927027127227327427527627727827928028128228328428528628728828929029112 行未改动3043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423432 行未改动34634734834935035135235335435535635735835936036136236336419 行未改动38438538638738838939039139239339439539639739839940040140236 行未改动4394404414424434444454464474484494504514524534544554564574584594604614624634649 行未改动47447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356420 行未改动一般说的浅谈是永远不会短的然后$qwq$本宝宝并不想讲实现原理 会用就行了呗然后方便起见,本文规定数组$a$的第$1$位为$a[0]$并且本文的所有$debug$为了方便看到我们$rope$长度之后的东西,会多输出若干位(看$debug$程序就懂了)42 行未改动__gnu_cxx::rope<int>ro;int n,m,p;void debug(int len){for(int i=0;i<=len;i++) printf("%d %d \n",i,ro[i]);//输出0~len这len+1位存的是啥puts("");return ;}```然后是添加删除,6 行未改动$push \_ front(n)$就是在最前面啦。然后测试一下:```cppint main(){scanf("%d",&n);5 行未改动testdata.in:51 2 3 4 5testdata.out:0 11 22 33 44 55 2576(或者别的)*/```7 行未改动return 0;}/*testdata.out0 51 42 33 24 15 0 (或者其它)*/```然后自己动手试试就更能理解了。(2).一个极其好用的插入函数:$insert(pos,x)$表示在第$pos$位插入一个数$n$(就是第$pos$位变成$n$,之前的第$pos$和$pos$后面的依次向右移)```cppint main(){scanf("%d",&n);10 行未改动1 2 3 4 511 10testdataout:0 11 22 33 44 55 312 (或者其他)0 11 102 23 34 45 5*/```所以$insert(0,n)$就等同于$push \_ front(n)$然后这个$insert()$还有个好处是这玩意支持整个数组同时插入$insert(pos,a)$(a为一个数组名)表示将a数组整个插入(从$a[0]$开始一直插入,直到某一位为$0$,不插入值为$0$的这一位)```cppint ll;int a[5]={0,0,0,0,0};int main(){16 行未改动311 12 131testdata1.out:0 11 112 123 134 25 36 47 58 0*//*testdata2.in:51 2 3 4 530 11 131testdata2.out:0 11 22 33 44 55 06 07 08 0*//*testdata3.in:51 2 3 4 5311 0 131testdata3.out:0 11 112 23 34 45 56 07 08 0*/```通过第一个数据我们见识到了$insert$的整段插入,第二,三个数据说明了$insert$会插入到下一位是$0$的这一位。当然,万一我们的某些题里一定要有$0$怎么办?我们不想全部插入怎么办?$insert(pos,a+x,a+y)$表示将$a$数组的$a[x]$开始到$a[y-1]$位总共$y-x$位一起在$pos$插入到$rope$里。```cppint ll;int a[5]={0,0,0,0,0};int x,y;int main()3 行未改动scanf("%d",&ll);for(int i=0;i<ll;i++) scanf("%d",&a[i]);scanf("%d%d",&x,&y);int pos;scanf("%d",&pos);ro.insert(pos,a+x,a+y);11 行未改动11 0 13 141 31testdata.out:0 11 02 133 24 35 46 57 08 09 0*/```下面开始讲删除操作删除的函数很少啊$……$$erase(pos)$一个很玄学的参数,表示从$ro[pos]$开始为第$1$位 ,往后删掉共$pos+1$位```cppint main(){scanf("%d",&n);12 行未改动61 2 3 4 5 60testdata1.out:0 11 22 33 44 55 66 40 (or any)0 21 32 43 54 65 1028672837 (or any)6 1432107587 (or any)*//*testdata2.in:61 2 3 4 5 61testdata2.out:0 11 22 33 44 55 66 40 (or any)0 11 42 53 64 0 (or any)5 0 (or any)6 0 (or any)*/2 行未改动61 2 3 4 5 64testdata3.out:0 11 22 33 44 55 66 40 (or any)0 11 22 33 44 1409315703 (or any)5 1028672837 (or any)6 1432107587 (or any)*/19 行未改动61 2 3 4 5 61 4testdata.out:0 11 22 33 44 55 66 40 (or else)0 11 62 6816088 (or else)3 0 (or else)4 0 (or else)5 0 (or else)6 0 (or else)36 行未改动61 2 3 4 5 61 3testdata.out:0 11 22 33 44 55 66 40 (or else)2 3 4 0 0*/```我们发现,我们在$testdata$里目的是将$1$到$3$替换成$11$然后输出中告诉我们了,我们把原先$1$到$3$的值也顺便覆盖掉了。其实相当于先$erase(pos,len)$然后在$insert(pos,newval)$$ replace(pos,st,\&x,en) $ 在$pos$插入$x$的前$en$位,其中重叠$st$位.(详见数据)```cppint a[5]={0,0,0,0,0};int main(){9 行未改动debug(n+4);//多输出点看看return 0;}```#### UPD:从这里起省略(or else)自己想吧```cpp/*testdata1.in:61 2 3 4 5 62 411 12 13 140 1testdata1.out:0 11 22 113 34 45 56 67 08 09 010 1658256847*//*testdata2.in:61 2 3 4 5 62 411 12 13 140 2testdata2.out:0 11 22 113 124 35 46 57 68 09 010 794409564*//*testdata3.in:61 2 3 4 5 62 411 12 13 141 4testdata3.out:0 11 22 113 124 135 146 47 58 69 010 0*//*testdata4.in:61 2 3 4 5 62 411 12 13 145 4testdata4.out:0 11 22 113 124 135 146 07 08 09 010 926115120*/```我们发现,当$st=0$时,相当于直接从$a$里插入长度为$en$,$st!=0$时是覆盖$st$位插入,如$testdata3$我们本应该是在$ro[6]$的$3$被覆盖掉了。20 行未改动123456742 行未改动50515253545556575859606 行未改动6768697071727374755 行未改动818283848586878889909192937 行未改动10110210310410510610710810911011111211311411511611711811912012112212310 行未改动13413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316 行未改动1801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402413 行未改动24524624724824925025125225311 行未改动26526626726826927027127227327427527627727827928028128228328428528628728828929029112 行未改动3043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423432 行未改动34634734834935035135235335435535635735835936036136236336419 行未改动38438538638738838939039139239339439539639739839940040140236 行未改动4394404414424434444454464474484494504514524534544554564574584594604614624634649 行未改动47447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356420 行未改动一般说的浅谈是永远不会短的然后$qwq$本宝宝并不想讲实现原理 会用就行了呗然后方便起见,本文规定数组$a$的第$1$位为$a[0]$并且本文的所有$debug$为了方便看到我们$rope$长度之后的东西,会多输出若干位(看$debug$程序就懂了)42 行未改动__gnu_cxx::rope<int>ro;int n,m,p;void debug(int len){for(int i=0;i<=len;i++) printf("%d %d \n",i,ro[i]);//输出0~len这len+1位存的是啥puts("");return ;}```然后是添加删除,6 行未改动$push \_ front(n)$就是在最前面啦。然后测试一下:```cppint main(){scanf("%d",&n);5 行未改动testdata.in:51 2 3 4 5testdata.out:0 11 22 33 44 55 2576(或者别的)*/```7 行未改动return 0;}/*testdata.out0 51 42 33 24 15 0 (或者其它)*/```然后自己动手试试就更能理解了。(2).一个极其好用的插入函数:$insert(pos,x)$表示在第$pos$位插入一个数$n$(就是第$pos$位变成$n$,之前的第$pos$和$pos$后面的依次向右移)```cppint main(){scanf("%d",&n);10 行未改动1 2 3 4 511 10testdataout:0 11 22 33 44 55 312 (或者其他)0 11 102 23 34 45 5*/```所以$insert(0,n)$就等同于$push \_ front(n)$然后这个$insert()$还有个好处是这玩意支持整个数组同时插入$insert(pos,a)$(a为一个数组名)表示将a数组整个插入(从$a[0]$开始一直插入,直到某一位为$0$,不插入值为$0$的这一位)```cppint ll;int a[5]={0,0,0,0,0};int main(){16 行未改动311 12 131testdata1.out:0 11 112 123 134 25 36 47 58 0*//*testdata2.in:51 2 3 4 530 11 131testdata2.out:0 11 22 33 44 55 06 07 08 0*//*testdata3.in:51 2 3 4 5311 0 131testdata3.out:0 11 112 23 34 45 56 07 08 0*/```通过第一个数据我们见识到了$insert$的整段插入,第二,三个数据说明了$insert$会插入到下一位是$0$的这一位。当然,万一我们的某些题里一定要有$0$怎么办?我们不想全部插入怎么办?$insert(pos,a+x,a+y)$表示将$a$数组的$a[x]$开始到$a[y-1]$位总共$y-x$位一起在$pos$插入到$rope$里。```cppint ll;int a[5]={0,0,0,0,0};int x,y;int main()3 行未改动scanf("%d",&ll);for(int i=0;i<ll;i++) scanf("%d",&a[i]);scanf("%d%d",&x,&y);int pos;scanf("%d",&pos);ro.insert(pos,a+x,a+y);11 行未改动11 0 13 141 31testdata.out:0 11 02 133 24 35 46 57 08 09 0*/```下面开始讲删除操作删除的函数很少啊$……$$erase(pos)$一个很玄学的参数,表示从$ro[pos]$开始为第$1$位 ,往后删掉共$pos+1$位```cppint main(){scanf("%d",&n);12 行未改动61 2 3 4 5 60testdata1.out:0 11 22 33 44 55 66 40 (or any)0 21 32 43 54 65 1028672837 (or any)6 1432107587 (or any)*//*testdata2.in:61 2 3 4 5 61testdata2.out:0 11 22 33 44 55 66 40 (or any)0 11 42 53 64 0 (or any)5 0 (or any)6 0 (or any)*/2 行未改动61 2 3 4 5 64testdata3.out:0 11 22 33 44 55 66 40 (or any)0 11 22 33 44 1409315703 (or any)5 1028672837 (or any)6 1432107587 (or any)*/19 行未改动61 2 3 4 5 61 4testdata.out:0 11 22 33 44 55 66 40 (or else)0 11 62 6816088 (or else)3 0 (or else)4 0 (or else)5 0 (or else)6 0 (or else)36 行未改动61 2 3 4 5 61 3testdata.out:0 11 22 33 44 55 66 40 (or else)2 3 4 0 0*/```我们发现,我们在$testdata$里目的是将$1$到$3$替换成$11$然后输出中告诉我们了,我们把原先$1$到$3$的值也顺便覆盖掉了。其实相当于先$erase(pos,len)$然后在$insert(pos,newval)$$ replace(pos,st,\&x,en) $ 在$pos$插入$x$的前$en$位,其中重叠$st$位.(详见数据)```cppint a[5]={0,0,0,0,0};int main(){9 行未改动debug(n+4);//多输出点看看return 0;}```#### UPD:从这里起省略(or else)自己想吧```cpp/*testdata1.in:61 2 3 4 5 62 411 12 13 140 1testdata1.out:0 11 22 113 34 45 56 67 08 09 010 1658256847*//*testdata2.in:61 2 3 4 5 62 411 12 13 140 2testdata2.out:0 11 22 113 124 35 46 57 68 09 010 794409564*//*testdata3.in:61 2 3 4 5 62 411 12 13 141 4testdata3.out:0 11 22 113 124 135 146 47 58 69 010 0*//*testdata4.in:61 2 3 4 5 62 411 12 13 145 4testdata4.out:0 11 22 113 124 135 146 07 08 09 010 926115120*/```我们发现,当$st=0$时,相当于直接从$a$里插入长度为$en$,$st!=0$时是覆盖$st$位插入,如$testdata3$我们本应该是在$ro[6]$的$3$被覆盖掉了。20 行未改动