work7

uno. 理解C++变量的作用域和生命周期

没有要求讲解我就简单注释了一下~

#include <iostream>
int main(){
  for (int i=0;i<10;i++){//alloc memory for i from stack
  // std::cout<<x<<std::endl;//wrong!x has not been announced
  int x;//alloc memory for x from stack
  std::cout<<i<<' '<<x<<std::endl;//ok!
}//x is poped while i is not
// std::cout<<i<<std::endl;//wrong! i is poped
}

due.理解堆和栈,两种内存的申请和释放的方式(栈)

#include<iostream>
using namespace std;
char *wrongdoing()

{

  char t[] = "wroingdoing";

  return t;
}
char *rightway()

{
  char *t = "rightway";
  return t;
}
int main()

{
  cout << wrongdoing()<<endl;//
  cout << rightway()<<endl;// 栈空间这么用就错了。。,已经被pop了
}

tre.理解堆和栈,两种内存的申请和释放的方式(堆)

#include<iostream>
using namespace std;
int *p,*q;
void test()

{
  int *a = new int(255);
  int b = 255;
  p = a;
  q = &b;
}
int main(void)

{
  test();
  cout << *p << endl;//堆上的还没有被清除,除非使用智能指针抑或是delete掉该指针
  cout << *q << endl;//栈上的被pop了
  return 0;
}

quattro. 理解unique_ptr和shared_ptr

unique_ptr:

1.只能进行移动操作无法进行复制赋值操作

2.保存指向某个对象的指针,当它本身被删除释放的时候(比如,离开了某个作用域),会使用给定的删除器释放它指向的对象。

3.如果在进行过程中抛出异常,同样它也会释放内存

shared_ptr:

  1. 有一个计数器保存了当前有多少智能指针对象共享了该内存块。
  2. 在运行了析构函数后,则将计数器减1,直到为1时释放内存
  3. 复制构造的时候计数器加1.

(感觉有点像信号量?)

cinque.草原民族风

#include <iostream>
#include <string>
#define deal(i) \
tmps="";\
for (j=lastword;j<i;j++)\
{\
tmps+=s[j];\
}\
ans[ansl++]=tmps;\
lastword=j+1
using namespace std;
int main()
{
int i,j;
string s,tmps;
std::cin>>s;
string ans[100];int ansl=0;
int lastword=0;
for (i=0;i<s.length();i++)
{
switch(s[i])
{
case ':':case '/':case '.':
deal(i);
if (ans[ansl-1].length()>0)cout<<ans[ansl-1]<<',';
default:
break;
}
}
deal(i);
if (ans[ansl-1].length()>0)cout<<ans[ansl-1];
}

sei .C++11风格,用的正则表达式

#include <regex>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
const regex pattern("(\\://|/|\\.)+");
string change = ",";
cin>>s;
cout<<regex_replace(s, pattern, change)<<endl;
}

 

 

posted on 2013-11-17 23:23  Yuzuka  阅读(166)  评论(1编辑  收藏  举报

导航