int main() {
  StrVec sv;
  print(sv);

  sv.push_back("s1"); print(sv);
  sv.push_back("s2"); print(sv);
  sv.push_back("s3"); print(sv);
  sv.push_back("s4"); print(sv);
  sv.push_back("s5"); print(sv);

  {
    StrVec sv2(sv); print(sv2);
    sv2.push_back("s6"); print(sv); print(sv2);
    sv.pop_back(); print(sv); print(sv2);
    sv = sv2; print(sv); print(sv2);
  }

  sv.reserve(sv.capacity() / 2); print(sv);
  sv.reserve(sv.capacity() * 2); print(sv);

  sv.resize(sv.size() + 2); print(sv);
  sv.resize(sv.size() + 2, "s7"); print(sv);
  sv.resize(sv.size() - 2); print(sv);
  sv.resize(sv.size() - 2, "s7"); print(sv);

  return 0;
}
示例#2
0
int main()
{
    StrVec vec;
    vec.reserve(6);
    std::cout << "capacity(reserve to 6): " << vec.capacity() << std::endl;

    vec.reserve(4);
    std::cout << "capacity(reserve to 4): " << vec.capacity() << std::endl;

    vec.push_back("hello");
    vec.push_back("world");

    vec.resize(4);

    for (auto i = vec.begin(); i != vec.end(); ++i)
        std::cout << *i << std::endl;
    std::cout << "-EOF-" << std::endl;

    vec.resize(1);

    for (auto i = vec.begin(); i != vec.end(); ++i)
        std::cout << *i << std::endl;
    std::cout << "-EOF-" << std::endl;

    StrVec vec_list{ "hello", "world", "pezy" };

    for (auto i = vec_list.begin(); i != vec_list.end(); ++i)
        std::cout << *i << " ";
    std::cout << std::endl;

    // Test operator==

    const StrVec const_vec_list{ "hello", "world", "pezy" };
    if (vec_list == const_vec_list)
        for (const auto &str : const_vec_list)
            std::cout << str << " ";
    std::cout << std::endl;

    // Test operator<
    const StrVec const_vec_list_small{ "hello", "pezy", "ok" };
    std::cout << (const_vec_list_small < const_vec_list) << std::endl;

    // Test []
    std::cout << const_vec_list_small[1] << std::endl;
}
void EventsGenerator::desiresCB(const hbba_msgs::DesiresSet::ConstPtr& msg)
{
	typedef std::vector<std::string> StrVec;
	typedef std::list<std::string> StrList;
	typedef std::vector<hbba_msgs::Desire> DesVec;
	typedef std::map<std::string,std::string> TypeMap;
	const DesVec& desires = msg->desires;
	TypeMap typeMap;
	StrVec ids;
	ids.reserve(desires.size());
	for (DesVec::const_iterator d = desires.begin(); d != desires.end(); ++d)
	{
		ids.push_back(d->id);
		typeMap[d->id] = d->type;
	}

    // First, remove desires not in it the current desires set.
    StrList del;
    for (Model::const_iterator i = model_.begin(); i != model_.end(); ++i)
    {
        const std::string& id = i->first;
        if (std::find(ids.begin(), ids.end(), id) == ids.end())
            del.push_back(id);
    }
    for (StrList::const_iterator i = del.begin(); i != del.end(); ++i)
    {
        const std::string& id = *i;
	
        event(id, model_[id].type, hbba_msgs::Event::DES_OFF);
        if (model_[id].flags & FLAG_INT)
            event(id, model_[id].type, hbba_msgs::Event::INT_OFF);
        if (model_[id].flags & FLAG_EXP)
            event(id, model_[id].type, hbba_msgs::Event::EXP_OFF);

        model_.erase(id);
    }

	// Then, generate events for new desires.
	for (StrVec::const_iterator i = ids.begin(); i != ids.end(); ++i)
	{
		const std::string& id = *i;
		if (model_.find(id) == model_.end())
		{
			model_[id].flags = FLAG_NONE;
			model_[id].type = typeMap[id]; //set type in the model
			event(id, model_[id].type, hbba_msgs::Event::DES_ON);
		}
	}

}