🔴 在 vector 里找最后一个,走了几步
(本条路线统一用 g++ -std=c++17 -O0 -Wall 编译)步数是**自己数的**,跟容器内部怎么实现无关:
#include <iostream>
#include <map>
#include <string>
#include <vector>
/* 同一件事:按名字找。步数是**自己数的**,跟容器内部怎么实现无关 */
int main() {
std::vector<std::string> v{"a", "b", "c", "d", "e"};
int vsteps = 0;
for (const auto &x : v) { vsteps++; if (x == "e") break; }
std::map<std::string, int> m{{"a",1},{"b",2},{"c",3},{"d",4},{"e",5}};
int found = (m.find("e") != m.end());
std::cout << v.size() << "/" << vsteps << "/" << found << "\n";
return 0;
}
全部评论