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。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值