记录学习过程中的点点滴滴
stl
std中二分查找函数集合
八 9th
1,binary_search() 用法:http://www.sgi.com/tech/stl/binary_search.html
2,lower_bound() 用法:http://www.sgi.com/tech/stl/lower_bound.html
3,upper_bound() 用法:http://www.sgi.com/tech/stl/upper_bound.html
4,equal_range() 用法:http://www.sgi.com/tech/stl/equal_range.html
STL使用总结之vector,map
十一 22nd
最近在做data mining的作业Apriori算法的实现是用到STL的知识,因为之前并未系统地用过STL库,所以在写程序时,遇到了很多问题,今天总结下,以便以后对STL有更好的使用
。
程序中主要用到两种数据结构:vector,map
一,vector介绍
<1>简单地说,vector是一个能够存放任意类型的动态数组,能够增加和删除数据。
<2>定义方法:vector<string> myvector;
<3>插入记录:
1,push_back(const T&);注意push_back()的参数是引用类型的,刚开始我以为在vector中插入数据只是插入一条记录的引用,并未复制该记录的实际值,后来才知道不是这样
的,vector重新为T开辟了一段内存空间,至于为什么参数是引用类型,我也不太清楚。最常用
2,myvector[2] = “mystring”;利用[]插入一条记录。
3,iterator insert(iterator pos,const T& x) //没怎么用过
void insert(iterator pos,InputIterator f, InputIterator l)
void insert(iterator pos, size_type n, const T& x)
<4>访问记录: 更多 >
为STL中的std::string添加trim函数的实现
十一 14th
代码如下:
//自定义trim函数
string trim(string& str)
{
string::size_type pos = str.find_last_not_of(‘ ‘);
if(pos != string::npos)
{
str.erase(pos + 1);
pos = str.find_first_not_of(‘ ‘);
if(pos != string::npos) str.erase(0, pos);
}
else str.erase(str.begin(), str.end());
return str;
std::string的split函数的自定义实现
十一 14th
std::string中未定义split函数,网上找到一种方法实现,分享之
//自定义实现split函数
void split(std::string& s, std::string& delim,std::vector< std::string >* ret)
{
size_t last = 0;
size_t index=s.find_first_of(delim,last);
while (index!=std::string::npos)
{
string tt = s.substr(last,index-last);
tt.
ret->push_back();
last=index+1;
index=s.find_first_of(delim,last);
}
if (index-last>0)
{
ret->push_back(s.substr(last,index-last));
}
}
近期评论