vector

Initializing a vector from a text file

Deadly 提交于 2021-02-04 19:44:10
问题 I am attempting to write a program which can read in a text file, and store each word in it as an entry in a string type vector. I am sure that I am doing this very wrong, but it has been so long since I have tried to do this that I have forgotten how it is done. Any help is greatly appreciated. Thanks in advance. Code: #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { vector<string> input; ifstream readFile; vector<string>::iterator

Efficiently create derangement of a vector in R

本小妞迷上赌 提交于 2021-02-04 19:27:09
问题 I'm looking into a way of efficiently creating a derangement (and conversely specific permutations) of a vector in R. As far as I've seen, there's no base function that does that and also there's not much about it here on SO. An obvious start is sample which creates a permutation of a vector. But I need this permutation to have no fixed points, hence be a derangement of the vector. For a nice explanation of this topic, see this Cross Validated post. This is my first approach: derangr <-

Details of C++ Vector push_back()

送分小仙女□ 提交于 2021-02-04 17:29:07
问题 I'm trying to debug a program, and in doing so have bumped up against my understanding of the C++ vector push_back() function. To illustrate my point, I've written the following short program: #include <iostream> #include <vector> #include <cstdlib> using std::cout; using std::endl; using std::vector; class Test { private: int mTestMember; public: Test(int val); Test(const Test&); int GetValue() const; }; Test::Test(int val) { cout << "Constructor\n"; mTestMember = val; } Test::Test(const

C++ Is it possible to cout a whole vector? [duplicate]

跟風遠走 提交于 2021-02-04 16:24:05
问题 This question already has answers here : How do I print out the contents of a vector? (30 answers) Closed 5 years ago . I need to cout a vector. Not just an element of it, but the whole thing. For example std::cout << vectorName; Something like that, hope it makes sense. Any ideas? Thanks in advance 回答1: You can either define a utility function like template <typename T> ostream& operator<<(ostream& output, std::vector<T> const& values) { for (auto const& value : values) { output << value <<

Print vector of vectors to ostream

邮差的信 提交于 2021-02-04 16:08:34
问题 Please consider the following code. I'm trying to output a vector of vectors to an ostream. #include <iterator> #include <iostream> #include <string> #include <vector> #include <algorithm> template<typename T> std::ostream &operator <<(std::ostream &os, const std::vector<T> &v) { using namespace std; copy(v.begin(), v.end(), ostream_iterator<T>(os, "\n")); return os; } int main() { using namespace std; vector<string> v1; cout << v1; vector<vector<string> > v2; cout << v2; return 0; } The

Print vector of vectors to ostream

怎甘沉沦 提交于 2021-02-04 16:08:34
问题 Please consider the following code. I'm trying to output a vector of vectors to an ostream. #include <iterator> #include <iostream> #include <string> #include <vector> #include <algorithm> template<typename T> std::ostream &operator <<(std::ostream &os, const std::vector<T> &v) { using namespace std; copy(v.begin(), v.end(), ostream_iterator<T>(os, "\n")); return os; } int main() { using namespace std; vector<string> v1; cout << v1; vector<vector<string> > v2; cout << v2; return 0; } The

Print vector of vectors to ostream

醉酒当歌 提交于 2021-02-04 16:07:09
问题 Please consider the following code. I'm trying to output a vector of vectors to an ostream. #include <iterator> #include <iostream> #include <string> #include <vector> #include <algorithm> template<typename T> std::ostream &operator <<(std::ostream &os, const std::vector<T> &v) { using namespace std; copy(v.begin(), v.end(), ostream_iterator<T>(os, "\n")); return os; } int main() { using namespace std; vector<string> v1; cout << v1; vector<vector<string> > v2; cout << v2; return 0; } The

Print vector of vectors to ostream

谁说胖子不能爱 提交于 2021-02-04 16:06:08
问题 Please consider the following code. I'm trying to output a vector of vectors to an ostream. #include <iterator> #include <iostream> #include <string> #include <vector> #include <algorithm> template<typename T> std::ostream &operator <<(std::ostream &os, const std::vector<T> &v) { using namespace std; copy(v.begin(), v.end(), ostream_iterator<T>(os, "\n")); return os; } int main() { using namespace std; vector<string> v1; cout << v1; vector<vector<string> > v2; cout << v2; return 0; } The

Using an object without copy and without a noexcept move constructor in a vector. What actually breaks and how can I confirm it?

早过忘川 提交于 2021-02-04 15:08:22
问题 I've checked a lot of move constructor/vector/noexcept threads, but I am still unsure what actually happens when things are supposed to go wrong. I can't produce an error when I expect to, so either my little test is wrong, or my understanding of the problem is wrong. I am using a vector of a BufferTrio object, which defines a noexcept(false) move constructor, and deletes every other constructor/assignment operator so that there's nothing to fall back to: BufferTrio(const BufferTrio&) =

Using an object without copy and without a noexcept move constructor in a vector. What actually breaks and how can I confirm it?

我与影子孤独终老i 提交于 2021-02-04 15:08:19
问题 I've checked a lot of move constructor/vector/noexcept threads, but I am still unsure what actually happens when things are supposed to go wrong. I can't produce an error when I expect to, so either my little test is wrong, or my understanding of the problem is wrong. I am using a vector of a BufferTrio object, which defines a noexcept(false) move constructor, and deletes every other constructor/assignment operator so that there's nothing to fall back to: BufferTrio(const BufferTrio&) =