예제 #1
0
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;
}
예제 #3
0
string
cp2ap (const string & cp_str)
{
  StrVec vec;
  vec.resize (100);		//这个地方必须用resize,因为resize有初始化,而reserve没有
  int front = 0;
  char ifs = '/';
  int pos1 = 0;
  int pos2 = 0;
  int len = cp_str.length ();
  string back ("/.."); //退回上一级
  string now ("/."); //当前目录
  while (pos1 < len && pos1 != cp_str.npos)
    {
      pos2 = cp_str.find (ifs, pos1 + 1);
      string tmp;
      if (pos2 == cp_str.npos)
	{
	  tmp = cp_str.substr (pos1, len - pos1);
	}
      else
	{
	  tmp = cp_str.substr (pos1, pos2 - pos1);
	}
      //cout<<tmp<<endl;
      if (tmp == back)
	{
		--front;
	}
      else if (tmp != now)
	{
	  vec[front++] = tmp;	// only if front < vec.size()
	}
      else;
      pos1 = pos2;
    }
  string ab_dir;
  for (int i = 0; i < front; ++i)
    ab_dir.append (vec[i]);
  return ab_dir;
}