vector 容器
vector是C++标准模版库(STL,Standard Template Library)中的部分内容。之所以认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单的说:vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。
使用vector容器之前必须加上<vector>头文件:#include<vector>;
vector属于std命名域的内容,因此需要通过命名限定:using std::vector;也可以直接使用全局的命名空间方式:using namespace std;
vector成员函数
c.push_back(elem)在尾部插入一个elem数据。
v.push_back(1);
c.pop_back()删除末尾的数据。
v.pop_back();
c.assign(beg,end)将[beg,end)一个左闭右开区间的数据赋值给c。
v1.push_back(10);
v1.push_back(20);
v2.push_back(30);
v2.assign(v1.begin(),v1.end());
c.assign (n,elem)将n个elem的拷贝赋值给c。
v.assign(5,10);//往v里放5个10
c.at(int index)传回索引为index的数据,如果index越界,抛出out_of_range异常。
cout << v.at(2) << endl;//打印vector中下标是2的数据
c.begin()返回指向第一个数据的迭代器。
c.end()返回指向最后一个数据之后的迭代器。
v.push_back(1);
v.push_back(2);
v.push_back(3);
vector<int>::iterator it;
for(it = v.begin();it!=v.end();it++){
cout << *it << "\t";
}
cout << endl;
c.rbegin()返回逆向队列的第一个数据,即c容器的最后一个数据。
c.rend()返回逆向队列的最后一个数据的下一个位置,即c容器的第一个数据再往前的一个位置。
v.push_back(1);
v.push_back(2);
v.push_back(3);
vector<int>::reverse_iterator it;
for(it = v.rbegin();it!=v.rend();it++){
cout << *it << "\t";
}
cout << endl;
c.capacity()返回容器中数据个数,翻倍增长。
v.push_back(1);
cout << v.capacity() << endl; // 1
v.push_back(2);
cout << v.capacity() << endl; // 2
v.push_back(3);
cout << v.capacity() << endl; // 4
c.clear()移除容器中的所有数据。
for(it = v.begin();it!=v.end();it++){
cout << *it << "\t";
}
v.clear();
for(it = v.begin();it!=v.end();it++){
cout << *it << "\t";
}
cout << endl;
c.empty()判断容器是否为空。
v.push_back(1);
v.push_back(2);
v.push_back(3);
if(!v.empty()){
cout << "v is not empty!" << endl;
}
c.erase(pos)删除pos位置的数据,传回下一个数据的位置。
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.erase(v.begin());
c.erase(beg,end)删除[beg,end)区间的数据,传回下一个数据的位置。
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.erase(v.begin(),v.end());
c.front()返回第一个数据。
c.back()传回最后一个数据,不检查这个数据是否存在。
v.push_back(1);
v.push_back(2);
v.push_back(3);
if(!vec.empty()){
cout << “the first number is:” << v.front() << endl;
cout << “thelast number is:” << v.back() << endl;
}
c.insert(pos,elem) 在pos位置插入一个elem的拷贝,返回插入的值的
C++ is a Huuuuuge Language.
The Path towards Expertise would be:
- Understanding C++ - Step 0
You have got to read The C++ Programming Language by Stroustrup, no way you can escape this. http://www.amazon.com/The-Progra...
- Understanding Histrory of C++ and Why come they designed the C++ Features - Step 1
The Design and Evolution of C++ Book from Stroustrup would help you.http://www.amazon.com/The-Design...
- Understanding Internals whats happening when you declare classes. -Step 2
Inside C++ Object Model by Stanley Lippman would help you.http://www.amazon.com/Inside-Obj...
- How to write Efficient Programs in C++ - Step 3
Agner Fog Optimization Manuals would give you Start. http://www.agner.org/optimize/
- How to write Correct C++ Programs - Step 4
Effective and More Effective C++ book would help, you cannot clear any C++ technical Interview without reading this. Better search for Scott Meyers in Google and Read Everything. He has huge insights for writing correct way of C++
- Understanding Design Patterns - Step 5
It would give basics to understand Huge C++ framework libraries such as Qt , Boost etc., The Gang of 4 Book would help you. http://www.amazon.com/Design-Pat...
- Understanding How to Create Efficient Frameworks - Step 6
API Design by Martin Reddy would give you a start. http://www.amazon.com/API-Design...
Phew .... We came so far without even touching Meta Programming and Templates.
- Meta Programming and Templates - Step 7
- Understanding How to become proficient in MetaProgramming - Step 8
For this you need to take different path instead of learning C++ , you would learn Haskell or CommonLisp. Haskell would be perfect. http://bartoszmilewski.com/2009/...
But i took the other one (Common Lisp). http://letoverlambda.com/
- Now its time to lose focus on C++ and Learn Compilers, Functional programming, meta programming etc..
- Implement C++ Compiler. (no mere souls done that if you can pull off you got big future in Google,Facebook etc..)
You may notice from Step 8, it become vague path. So I would say after Step 8 you need to invent your own Path.
Did I mention to read C++ 0x11 oh god So much to read but life is too short .
So my opinion would be C++ language is gonna stay for foreseeable future unless we work on computer which is not based on Von neumann architecture.
So better invest your 10 years to expertise C++ and Computer programming.
It does pay off, you can demand Good Salary, no matter what hype is (VB, Java, C# and now Go). Its tested against Time .
本文链接
cocoa中虽然有[[NSFileManager defaultManager] fileExistsAtPath:filename]来检查文件是否可写的方法,但是对文件目录却不起作用,没办法只好自己写一个比较山寨的方法:
bool IsDirectoryWritable(NSString *dir)
{
bool result = false;
if(![[NSFileManager defaultManager] fileExistsAtPath:dir])
return result;
NSString* fileName = [dir stringByAppendingFormat:@"/ _#t.txt"];
NSData *data = [fileName dataUsingEncoding:NSUTF8StringEncoding];
[data writeToFile:fileName atomically:NO];
result = [[NSFileManager defaultManager] fileExistsAtPath:fileName];
if(result)
{
[[NSFileManager defaultManager] removeItemAtPath:fileName error:NULL];
}
return true;
}
这个方法的不好之处就是有可能用于尝试的fileName可能已经存在(虽然已经起的很奇怪了),这样会导致返回结果不准确,也有可能测试文件创建成功了但是删除却失败了,那么也会导致下次测试不准确,。如果哪位高人有更好的办法,麻烦指教。