本文整理汇总了C++中MyVector::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ MyVector::insert方法的具体用法?C++ MyVector::insert怎么用?C++ MyVector::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MyVector
的用法示例。
在下文中一共展示了MyVector::insert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main (int argc, char * const argv[]) {
bool use_non_characters = false;
// cout << "use non-characters?:";
// char c[2];
// cin >> c;
// if (c[0] == 'y') use_non_characters = true;
char filename[] = "words.txt";
// vector<string> lines;
MyVector<string> lines;
cout << "reading words from " << filename << endl;
ifstream data_in(filename, ios_base::in);
while(!data_in.eof()){
string tmp_line;
getline(data_in,tmp_line);
//lines.push_back(tmp_line);
lines.add(tmp_line);
}
data_in.close();
cout << "readed words: " << endl;
// for (vector<string>::iterator it=lines.begin(); it < lines.end(); it++) {
for (MyVector<string>::iterator it=lines.begin(); it < lines.end(); it++) {
cout << *it << endl;
if (contains_non_characters(*it) && !use_non_characters) {
cout << "contains non characters... aborting program" << endl;
return 0;
}
}
cout << endl;
cout << "input words(to exit type enter):" << endl;
string line;
while (getline(cin,line) && line != "exit"){
// cout << "contains non-characters : " << contains_non_characters(line) << endl;
if (!use_non_characters && contains_non_characters(line)) {
cout << "containst non-characters, ignoring word" << endl;
continue;
}
for (MyVector<string>::iterator it=lines.begin(); it < lines.end(); it++) {
if (line.compare(*it) < 0){
lines.insert(*it, line);
break;
}
}
}
cout << "words input ended" << endl;
cout << "result list: " << endl;
for (MyVector<string>::iterator it=lines.begin(); it < lines.end(); it++)
cout << *it << endl;
//записывам список обратно в файл
// ofstream data_out(filename, ios_base::out | ios_base::trunc);
// for (vector<string>::iterator it=lines.begin(); it < lines.end(); it++)
// data_out << *it << endl;
// data_out.close();
return 0;
}
示例2: main
int main(){
MyVector<string> vec;
vec.push_back("CS14");
vec.push_back("World");
vec.insert("Hello", 0);
cout << vec.front() << endl; // "Hello"
cout << vec.back() << endl; // "World"
vec.pop_back();
cout << vec.back() << endl; // "CS14"
vec.pop_back();
cout << vec.back() << endl; // "Hello"
MyVector<int> another_vec;
another_vec.push_back(42);
cout << another_vec.front() << endl; //42
return 0;
}