Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
3 Star 10 Fork 6

secondtonone1/cpplearn

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
cpplearn
/
src
/
pointer_.cpp
cpplearn
/
src
/
pointer_.cpp
pointer_.cpp 8.36 KB
Copy Edit Raw Blame History
secondtonone1 authored 2022年01月20日 11:00 +08:00 . master
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
#include "../inc/pointer_.h"
#include <algorithm>
#include <numeric>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <functional>
#include <memory>
#include <exception>
using namespace std;
void use_pointer()
{
//开辟整形指针,指向一个值为5的元素
int *pint = new int(5);
//开辟指向字符串的指针
string *pstr = new string("hello zack");
//释放pint指向的空间
if (pint != nullptr)
{
delete pint;
pint = nullptr;
}
//释放指针指向的空间。
if (pstr != nullptr)
{
delete pstr;
pstr = nullptr;
}
}
void use_sharedptr()
{
//定义一个指向int类型的shared_ptr
shared_ptr<int> psint;
//定义一个指向string类型的shared_ptr
shared_ptr<string> psstr;
//定义一个指向list<int>的shared_ptr
shared_ptr<list<int>> psilist;
//我们定义一个指向整形5得指针
auto psint2 = make_shared<int>(5);
//判断智能指针是否为空
if (psint2 != nullptr)
{
cout << "*psint2 is " << *psint2 << endl;
}
auto psstr2 = make_shared<string>("hello zack");
if (psstr2 != nullptr && !psstr2->empty())
{
cout << "*psstr2 is " << *psstr2 << endl;
}
int *pint = psint2.get();
cout << "*pint is " << *pint << endl;
//将psint2赋值给psint3,他们底层的内置指针相同
// psint3和psint2引用计数相同,引用计数+1,都为2
shared_ptr<int> psint3 = psint2;
//打印引用计数
cout << "psint2 usecount is " << psint2.use_count() << endl;
cout << "psint3 usecount is " << psint3.use_count() << endl;
// psint3引用计数为1
psint3 = make_shared<int>(1024);
// psint2引用计数-1,变为1
//打印引用计数
cout << "psint2 usecount is " << psint2.use_count() << endl;
cout << "psint3 usecount is " << psint3.use_count() << endl;
}
StrBlob::StrBlob()
{
data = make_shared<vector<string>>();
}
StrBlob::StrBlob(const StrBlob &sb)
{
data = sb.data;
}
StrBlob::StrBlob(const initializer_list<string> &li)
{
data = make_shared<vector<string>>(li);
}
StrBlob &StrBlob::operator=(const StrBlob &sb)
{
if (&sb != this)
{
this->data = sb.data;
}
return *this;
}
//检测i是否越界
void StrBlob::check(size_type i, const string &msg) const
{
if (i >= data->size())
{
throw out_of_range(msg);
}
}
string &StrBlob::front()
{
check(0, "front on empty StrBlob");
return data->front();
}
string &StrBlob::back()
{
check(0, "back on empty StrBlog");
return data->back();
}
void StrBlob::pop_back()
{
check(0, "back on pop_back StrBlog");
data->pop_back();
}
void StrBlob::printCount()
{
cout << "shared_ptr use count is " << data.use_count() << endl;
}
void test_StrBlob()
{
StrBlob strblob1({"hello", "zack", "good luck"});
StrBlob strblob2;
try
{
auto str2front = strblob2.front();
}
catch (std::out_of_range &exc)
{
cout << exc.what() << endl;
}
catch (...)
{
cout << "unknown exception" << endl;
}
strblob2 = strblob1;
auto str1front = strblob1.front();
cout << "strblob1 front is " << str1front << endl;
strblob2.printCount();
strblob1.printCount();
}
void delfunc(string *p)
{
if (p != nullptr)
{
delete (p);
p = nullptr;
}
cout << "self delete" << endl;
}
void shared_ptrnew()
{
auto psint = shared_ptr<int>(new int(5));
auto psstr = shared_ptr<string>(new string("hello zack"));
//错误,不能用内置指针隐式初始化shared_ptr
// shared_ptr<int> psint2 = new int(5);
//正确,显示初始化
shared_ptr<string> psstr2(new string("good luck"));
//可以通过一个shared_ptr 构造另一个shared_ptr
shared_ptr<string> psstr3(psstr2);
cout << "psstr2 use count is " << psstr2.use_count() << endl;
cout << "psstr3 use count is " << psstr3.use_count() << endl;
//可以设置新的删除函数替代delete
shared_ptr<string> psstr4(new string("good luck for zack"), delfunc);
}
void process(shared_ptr<int> psint)
{
cout << "psint data is " << *psint << endl;
}
void bad_use_sharedptr2()
{
int *p = new int(5);
process(shared_ptr<int>(p));
//危险,p已经被释放,会造成崩溃或者逻辑错误
cout << "p data is " << *p << endl;
}
void bad_use_sharedptr3()
{
shared_ptr<int> p(new int(5));
//通过p获取内置指针q
//注意q此时被p绑定,不要手动delete q
int *q = p.get();
{
//两个独立的shared_ptr m和p都绑定q
auto m = shared_ptr<int>(q);
}
//上述}结束则m被回收,其绑定的q也被回收
//此时使用q是非法操作,崩溃或者逻辑错误
cout << "q data is " << *q << endl;
}
void good_use_sharedptr()
{
shared_ptr<int> p(new int(5));
//通过p获取内置指针q
//注意q此时被p绑定,不要手动delete q
int *q = p.get();
{
// m和p的引用计数都为2
shared_ptr<int> m(p);
}
//上述}结束则m被回收,其绑定的q也被回收
//此时使用q是非法操作,崩溃或者逻辑错误
cout << "q data is " << *q << endl;
}
void reset_shared()
{
shared_ptr<int> p(new int(5));
// p重新绑定新的内置指针
p.reset(new int(6));
//如果引用计数为1,unique返回true
if (!p.unique())
{
//还有其他人引用,所以我们为p指向新的内存
p.reset(new int(6));
}
// p目前是唯一用户
*p = 1024;
}
void execption_shared()
{
shared_ptr<string> p(new string("hello zack"));
//此处导致异常
int m = 5 / 0;
//即使崩溃也会保证p被回收
}
void delfuncint(int *p)
{
cout << *p << " in del func" << endl;
}
void delfunc_shared()
{
int p = 6;
shared_ptr<int> psh(&p, delfuncint);
}
void use_uniqueptr_p()
{
//指向double类型的unique指针
unique_ptr<double> udptr;
//一个指向int类型的unique指针
unique_ptr<int> uiptr(new int(42));
// unique不支持copy
// unique_ptr<int> uiptr2(uiptr);
// unique不支持赋值
// unique_ptr<int> uiptr3 = uiptr;
}
void use_uniqueptr_reset()
{
//定义一个upstr
unique_ptr<string> upstr(new string("hello zack"));
// upstr.release()返回其内置指针,并将upstr置空
// 用upstr返回的内置指针初始化了upstr2
unique_ptr<string> upstr2(upstr.release());
unique_ptr<string> upstr3(new string("hello world"));
//将upstr3的内置指针转移给upstr2
// upstr2放弃原来的内置指针,指向upstr3返回的内置指针。
upstr2.reset(upstr3.release());
}
void use_uniqueptr_del()
{
//定义一个upstr
unique_ptr<string> upstr(new string("hello zack"));
//获取upstr的内置指针
string *inerp = upstr.release();
//因为此时upstr已经通过release交出内置指针使用权
//所以要手动释放内置指针的内存
delete inerp;
}
unique_ptr<int> clone_unique(int a)
{
return unique_ptr<int>(new int(a));
}
void use_uniqueptr_clone()
{
int a = 1024;
unique_ptr<int> mp = clone_unique(a);
cout << *mp << endl;
}
void unique_deleter(int *p)
{
cout << "this is unique deleter" << endl;
cout << "inner pointer data is " << *p << endl;
}
void use_uniqueptr()
{
unique_ptr<int, decltype(unique_deleter) *> mp(new int(1024), unique_deleter);
}
void use_weakptr_count()
{
//构造shared_ptr
auto psint = make_shared<int>(1024);
//用shared_ptr构造weak_ptr
weak_ptr<int> pwint(psint);
//打印shared_ptr的引用计数
cout << "shared_ptr use count is " << psint.use_count() << endl;
}
weak_ptr<int> clone_weakptr(int num)
{
shared_ptr<int> psint(new int(num));
return weak_ptr<int>(psint);
}
void use_weakptr_expired()
{
auto wptr = clone_weakptr(1024);
if (wptr.expired())
{
cout << "wptr inner pointer has been deleted" << endl;
}
else
{
cout << "wptr inner pointer data is " << *(wptr.lock()) << endl;
}
}
void use_weakptr()
{
shared_ptr<int> psint(new int(1022));
//也可以通过赋值,将shared_ptr赋值给weak_ptr
weak_ptr<int> pwint = psint;
//通过weak_ptr生成shared_ptr
shared_ptr<int> psint2 = pwint.lock();
cout << "psint use count is " << psint.use_count() << endl;
cout << "psint2 use count is " << psint2.use_count() << endl;
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/secondtonone1/cpplearn.git
git@gitee.com:secondtonone1/cpplearn.git
secondtonone1
cpplearn
cpplearn
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /