stdvector

When adding an element to a vector, how to know that a copy of the object is going to be made?

浪子不回头ぞ 提交于 2019-12-11 08:20:00
问题 I have an object called LastQueryInfo lastQuery in my class. Every time this object changes, I add it to a vector called history . Initially, when I did history.push_back(lastQuery) I didn't know what would happen - is the vector going to make a copy of the object? or is it going to keep a reference to it? So if later I modify lastQuery, are all the objects (assuming they are references) in the history vector going to be modified? After some testing, I found that history.push_back(lastQuery)

converting matrix to std::vector

ぃ、小莉子 提交于 2019-12-11 06:09:28
问题 I have the following matrix: unsigned wins[8][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } }; how to convert it into a std::vector? 回答1: You can use the two iterator constructor to instantiate a vector with a copy of of the data in wins : unsigned* start = &wins[0][0]; std::vector<unsigned> vwins(start, start + (8 * 3)); This relies on pointer arithmetic, the fact that pointers are iterators, and the fact that 2D arrays are

Removing an item from an std:: vector

╄→гoц情女王★ 提交于 2019-12-11 05:28:37
问题 In the 1st code snippet below, I am trying to remove an element from a vector within a member function based on a static condition function fed into std::remove function. Then I am getting lots of template errors shown in the 2nd snippet. Can you please tell me what I am missing? SNIPPET 1 (CODE) void removeVipAddress(std::string &uuid) { struct RemoveCond { static bool condition(const VipAddressEntity & o) { return o.getUUID() == uuid; } }; std::vector<VipAddressEntity>::iterator last = std:

NSMutableArray to std::vector

白昼怎懂夜的黑 提交于 2019-12-11 05:27:58
问题 Is it possible to convert the contents of an NSMutableArray to a std::vector? And if so, should this be done in the Objective-C or C++ code? 回答1: You can create a vector with any Objective-C type. For example to store a NSString instance into a vector, you can use next code: NSMutableArray<NSString*>* array = [@[@"1", @"2"] mutableCopy]; __block std::vector<NSString*> vectorList; vectorList.reserve([array count]); [array enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx,

How to move the later half of a vector into another vector?

折月煮酒 提交于 2019-12-11 04:55:39
问题 I have a vector and I would like to efficiently break out the second half of the vector into another vector using STL algorithms. Here is one way I see to do this, but expect there are more efficient and succinct answers, or at the least, one that uses the stl algorithms: std::vector<Entry> &entries = someFunction(); int numEntries = entries.size(); // Assume numEntries is greater than or equal to 2. std::vector<Entry> secondEntries; std::vector<Entry>::iterator halfway = entries.begin() +

How to tell if std::vector resized itself, and how to account for pointers to values inside the vector no longer being valid?

亡梦爱人 提交于 2019-12-11 04:27:20
问题 I understand that if a std::vector is resized (I believe only increased in size) that the memory location of the vector is re-located in an effort to find a new location in heap memory that will actually fit the new size. In that event, if I have pointer A, B, and C that previously pointed to elements inside the vector, they will be pointing to the old, de-allocated memory locations and no longer be valid. I wanted to know if A: If it was possible to be notified when such an event happens,

What's the status of std::vector::data()?

↘锁芯ラ 提交于 2019-12-11 03:38:55
问题 I just realized that I've been using std::vector::data() out of similarity with std::string, but a colleague pointed out that it's not standard. Apparently Gcc implements it, but looking at its include files, I found this comment: // _GLIBCXX_RESOLVE_LIB_DEFECTS // DR 464. Suggestion for new member functions in standard containers. // data access My questions are: is this method widely implemented by other compilers? is it included in C++0x? (I also wonder what DR 464 is, and also _GLIBCXX

Vector of comma separated token to const char**

冷暖自知 提交于 2019-12-11 02:29:06
问题 I am trying to convert a comma separated string to vector of const char*. With the following code, by expected output is ABC_ DEF HIJ but I get HIJ DEF HIJ Where am I going wrong? Code: #include <iostream> #include <boost/tokenizer.hpp> #include <vector> #include <string> using namespace std; int main() { string s("ABC_,DEF,HIJ"); typedef boost::char_separator<char> char_separator; typedef boost::tokenizer<char_separator> tokenizer; char_separator comma(","); tokenizer token(s, comma);

why is the sort() not working?

帅比萌擦擦* 提交于 2019-12-11 01:57:59
问题 I got this simple program read in a string like "13 11 9 10". I wanna split string then sort them. however the sort() seems not working, any help? input: 13 11 9 10 , output: 13 11 9 10 Thanks! #include <string> #include <sstream> #include <iostream> #include <algorithm> #include <vector> using namespace std; vector<int> split(string s) { istringstream iss(s); vector<int> result; do{ string sub; iss>>sub; if(sub!="") result.push_back((int)atoi(sub.c_str())); }while(iss); return result; } int

unexpected result iterating over a boost::python vector_indexing_suite

杀马特。学长 韩版系。学妹 提交于 2019-12-11 00:18:10
问题 I have wrapped successfully a class named Composite. This class has the following method: std::vector<Composite*> Composite::getChildren(); I tried to wrap the returned std::vector using the vector_indexing_suite, in this way: [snippet] typedef std::vector<Composite*> CompositeArray; BOOST_PYTHON_MODULE(composite) { class_<CompositeArray>("CompositeArray") .def(vector_indexing_suite<CompositeArray, true>()); class_<Composite>("Composite", init<>()) ... more wrapper .def("getChildren",