#include <iostream>#include <vector>using namespace std;int main(int argc, char *argv[]) { vector<int> myvector {1, 2, 3, 4, 5, 6, 7, 8}; cout << "myvector: "; for (size_t i = 0; i < myvector.size(); ++i) { cout << " " << myvector[i]; } cout << endl; return 0;}
#include <iostream>#include <vector>using namespace std;int main(int argc, char *argv[]) { vector<int> myvector {1, 2, 3, 4, 5, 6, 7, 8}; size_t size = myvector.size(); cout << "myvector: "; for (size_t i = 0; i < size; ++i) { cout << " " << myvector[i]; } cout << endl; return 0;}
C++ (Qt)std::vector <int> vec;// QVector <int> vec;// как-то заполнили вектор...size_t count = vec.size();for (size_t i = 0; i < count; ++i) { int & val = vec[i]; if (val < 0) vec.push_back(val); val = 0;}
#include <iostream>#include <vector>using namespace std;int main(int argc, char *argv[]) { vector<int> myvector {1, 2, 3, 4, 5, 6, 7, 8}; cout << "myvector: "; for (auto iter = myvector.cbegin(); iter != myvector.cend(); ++iter) { cout << " " << *iter; } cout << endl; cout << "myvector: "; for (auto& i : myvector) { cout << " " << i; } cout << endl; return 0;}
void func(vector<int> &vec) { size_t count = vec.size(); for (size_t i = 0; i < count; ++i) { int & val = vec[i]; if (val < 0) vec.push_back(val); val = 0; } cout << "!!!!" << " " << vec.size() << endl;}
void newtestclass::testFunc_002() { vector<int> vec; // Заполняем массив vec.push_back(-1); vec.push_back(3); vec.push_back(6); vec.push_back(7); vec.push_back(1); // Обрабатываем элементы массива func(vec); // Проверяем размер массива int expectedSize = 6; int actualSize = vec.size(); CPPUNIT_ASSERT_EQUAL(expectedSize, actualSize);}