本文整理汇总了C++中Array_::allocated方法的典型用法代码示例。如果您正苦于以下问题:C++ Array_::allocated方法的具体用法?C++ Array_::allocated怎么用?C++ Array_::allocated使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Array_
的用法示例。
在下文中一共展示了Array_::allocated方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testInsert
void testInsert() {
const int data1[3] = {7, -2, 3};
const int data2[4] = {101, 121, -111, 321};
int wdata[3] = {99, 9999, 999}; // writable
Array_<int> a(data2,data2+4); // copy the data
SimTK_TEST(&a[0] != data2);
ArrayViewConst_<int> avc(data2, data2+4); // share the data
SimTK_TEST(&avc[1] == data2+1);
Array_<int> aw(wdata, wdata+3, DontCopy()); // shared
SimTK_TEST(&aw[0] == wdata);
// Can't insert into non-owner.
SimTK_TEST_MUST_THROW(aw.insert(aw.begin(), avc.begin(), avc.end()));
// Unless we're inserting zero elements; that's allowed.
aw.insert(&aw[1], avc.begin(), avc.begin());
Array_<int> ac(data1, data1+3);
std::vector<int> vc(data1, data1+3);
ac.insert(&ac[1], &a[1], &a[1]+2);
vc.insert(vc.begin()+1, &a[1], &a[1]+2);
SimTK_TEST(ac.size()==5);
SimTK_TEST(ac == vc); // 7, 121, -111, -2, 3
// insert vc onto beginning and end of ac
ac.insert(ac.begin(), vc.begin(), vc.end());
ac.insert(ac.end(), vc.begin(), vc.end());
SimTK_TEST(ac.size()==15);
SimTK_TEST(ac(0,5)==vc && ac(5,5)==vc && ac(10,5)==vc);
// Shrink ac back down to 5 again.
ac.erase(ac.begin()+2, ac.begin()+12);
SimTK_TEST(ac == vc);
SimTK_TEST(ac.allocated() >= 15);
ac.shrink_to_fit();
SimTK_TEST(ac.allocated() < 15);
SimTK_TEST(ac == vc); // make sure we didn't lose the data
// Now try some null insertions
Array_<int> null;
ac.insert(ac.begin(), null.begin(), null.end());
ac.insert(ac.begin()+2, null.begin(), null.end());
ac.insert(ac.end(), null.begin(), null.end());
ac.insert(ac.begin(), 0, 929);
ac.insert(ac.begin()+2, 0, 929);
ac.insert(ac.end(), 0, 929);
SimTK_TEST(ac == vc);
ArrayView_<int> null2;
null.insert(null.begin(), null2.begin(), null2.end());
SimTK_TEST(null.empty() && null2.empty());
// How about inserting into a null array?
null.insert(null.begin(), 3, 987);
SimTK_TEST(null == std::vector<int>(3,987));
null.deallocate(); // back to null
SimTK_TEST(null.data()==0 && null.size()==0 && null.allocated()==0);
null.insert(null.begin(), ac.begin(), ac.end());
SimTK_TEST(null == vc);
null.deallocate();
// Fill in a bunch of 1000's in the middle, erase the beginning and
// end, and make sure we see just the 1000's.
ac.insert(ac.begin()+2, 99, 1000);
ac.erase(ac.begin(), ac.begin()+2);
ac.erase(ac.end()-3, ac.end());
SimTK_TEST(ac == Array_<int>(99,1000));
}