加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

about C and C++

(2012-04-17 15:35:44)
标签:

it

char buf[10]={0};
buf[0]='a';
buf[1]='b';
cout<<buf<<endl;
int a = 10;
list<edge> *graph = new list<edge>[a];
edge e;
e.followee=10;
e.follower = 11;
graph[1].push_back(e);
char buf[100]={0};
char labelbuf[100]={0};
node lnode;
ifs.getline(buf,100);
//if(strlen(buf)!=0)
sscanf(buf,"%ld%s", &lnode.nodeid, labelbuf);
//scanf %s takes a char* not a string as para.
lnode.label = labelbuf;
cout<<"node: "<<lnode.nodeid<<"\tlabel: "<<lnode.label<<endl;

//itr != labellist.end();
for(itr=labellist.begin(); itr<labellist.end(); itr++){
if((*itr).nodeid == nodeid) return true;
else continue;
}
list<datatype> * followergraph = new list<datatype>[followers+1];
constructFollowerGraph(filename1,followers, followergraph);
//add a sentinel! PLUS ONE!

//how to use a iterator
list<int> intlist;
intlist.push_front(3);
intlist.push_front(4);
list<int>::iterator itr4intlist=intlist.begin();
itr4intlist++;
cout<<*(itr4intlist-1)<<endl;
return 0;
//iterator has five types: input, output, forward, bidirectional and random access;
//only the last type can be used as the form of "itr+n", where itr is a random access
// iterator and n is an integer

//map instance
typedef pair<long, string> NODE;
//constructor
map<long, string> label_map;
//insert a pair
long nodeid = 123;
string label="abc";
label_map[123]=label;
label_map.insert(NODE(nodeid, label));
//find a key using iterator
map<long, string>::iterator itr;
itr = label_map.find(nodeid);
cout<<(*itr).first<<endl;
//check the pointer of iterator
cout<<&itr<<endl;
map<long, string>::iterator *pitr = &itr;
//two ways to visit member variables
cout<<(*pitr)->first<<endl;
cout<<(**pitr).first<<endl;

error C2679: 二进制“=”: 没有找到接受“std::_Tree<_Traits>::const_iterator”类型的右操作数的运算符(或没有可接受的转换)
void printLabel(const map<datatype, string>& labelMap){ //as here comes a const
map<datatype, string>::iterator itr; //here a movable but unchangeable const_iterator must

for(itr = labelMap.begin(); itr != labelMap.end(); itr++){
cout<<(*itr).first<<'\t'<<(*itr).second<<endl;
}
}

 const_iterator vs const ... iterator
 vector<int> ivec;
  vector<int>::const_iterator citer1 = ivec.begin(); //movable
  const vector<int>::iterator citer2 = ivec.begin(); //changeable
  *citer1 = 1; //error
  *citer2 = 1; //right
  ++citer1; //right
  ++citer2; //error
  
  typedef list<map<datatype, string>::iterator *> adjList;
  //this definition is OK!
  
set<int> intset;
pair<set<int>::iterator, bool> *status = NULL;
status = &intset.insert(1);
cout<<&(status->first)<<"\t"<<status->second<<endl; 
//print 001DFB00
status = &intset.insert(2);
cout<<&(status->first)<<"\t"<<status->second<<endl;
//print 001DFB24
status = &intset.insert(1);
cout<<&(status->first)<<"\t"<<status->second<<endl;
//print 001DFB48
//when insert a member into a set, create a new member and then insert it
//case is not that check whether the member and then create a new member
//more attention: size of a pair is bigger than expected

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有