c++作业小结

1. 对list元素排序应该用它的成员函数sort,而不是std::sort

list虽然属于顺序容器(有头有尾),但它和其它顺序容器(vector、deque)最大的不同在于list不支持随机访问,只能顺序访问。如果试图用std::sort对list中的元素进行排序,编译会报错error: invalid operands to binary expression ('iterator_type' (aka 'std::_List_iterator >') and 'iterator_type') { return __y.base() - __x.base(); },原因是std::sort需要用随机访问迭代器而list不支持,所以应该用list自带的sort进行排序。

std::sort requires random access iterators, which std::list does not provide. Consequently, std::list and std::forward_list implement their own member functions for sorting which work with their weaker iterators. 

Moreover, the member functions can take advantage of the special nature of the list data structure by simply relinking the list nodes, while the standard algorithm has to perform something like swap (or something to that effect), which requires object construction, assignment and deletion.

Note that remove() is a similar case: The standard algorithm is merely some iterator-returning rearrangement, while the list member function performs the lookup and actual removal all in one go; again thanks to being able to take advantage of the knowledge of the list's internal structure.

 2. Xcode读写文件位置的设置

Product->Scheme->Edit Scheme: 左边选Run,上面选Option,在Working Dictionary选项里面,打钩 Using cusom working dictionary,下面地址填当前project文件夹就好。之后生成的文件会在当前project文件夹,要读的文件也是放到当前project文件夹。

3. 判断文件结尾

若是每次用ch=fgetc(fp)读取单个字符,则用while(ch != EOF)就行;若是每次用fgets(str,sizeof(str),fp)读一个字符串,则用while(!feof(fp))。使用feof需要注意的是,若文件为空它也会先读一次再跳出循环,解决办法是先读一个字符,若为EOF说明是空文件则关闭文件退出,否则rewind回到文件开头正常读取。

4. Xcode编译报错:undefined symbols for architecture xxx

大部分情况是因为缺少了需要的.dylib文件,或没添加需要的framework。

 

你可能感兴趣的:(c++)