本文整理汇总了C++中StrVec::resize方法的典型用法代码示例。如果您正苦于以下问题:C++ StrVec::resize方法的具体用法?C++ StrVec::resize怎么用?C++ StrVec::resize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StrVec
的用法示例。
在下文中一共展示了StrVec::resize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
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: main
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: back
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;
}