Can I use const in vectors to allow adding elements, but not modifications to the already added?

后端 未结 14 640
既然无缘
既然无缘 2020-12-01 04:54

My comments on this answer got me thinking about the issues of constness and sorting. I played around a bit and reduced my issues to the fact that this code:



        
14条回答
  •  情书的邮戳
    2020-12-01 05:26

    Although this doesn't meet all of your requirements (being able to sort), try a constant vector:

    int values[] = {1, 3, 5, 2, 4, 6};
    const std::vector IDs(values, values + sizeof(values));
    

    Although, you may want to use a std::list. With the list, the values don't need to change, only the links to them. Sorting is accomplished by changing the order of the links.

    You may have to expend some brain power and write your own. :-(

提交回复
热议问题