[英]std::copy doesn't copy vector in C++
要查找僅包含 0 和 1 的所有固定長度序列,我使用以下代碼:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void print_array(vector<string> arr) {
cout << '[';
int n = arr.size();
for (size_t i = 0; i < n; i++) {
cout << arr[i];
if (i < (n - 1)) {
cout << ", ";
}
}
cout << ']' << endl;
}
vector<string> get_variants(int n) {
vector<string> result = {"0", "1"};
vector<string> temp;
temp.reserve(2);
result.reserve(2);
for (int i=0; i < (n - 1); ++i) {
copy(result.begin(), result.end(), temp.end()); // [1]
for (int j=0; j < result.size(); ++j) {
temp[j] += "0";
result[j] += "1";
}
copy(temp.begin(),temp.end(), result.end());
temp.clear();
}
return result;
}
int main(int argc, char const *argv[]) {
int n;
cin >> n;
vector<string> maybe = get_variants(n);
print_array(maybe);
return 0;
}
但是向量temp
是空的,在我標記為 [1] 的行中復制之前以及之后。 所以,我的程序的 output 是[0111, 1111]
。 我做錯了什么?
比使用std::copy
更直接的方法是使用.insert()
:
temp.insert(temp.end(), result.begin(), result.end()); //1
...
result.insert(result.end(), temp.begin(), temp.end()); // 2nd copy
您正在寫信給temp.end()
和result.end()
。 這些迭代器代表“一個結束”,因此寫入這些迭代器是未定義的行為。
您似乎正在尋找std::back_inserter
。 這將創建一個迭代器,該迭代器將在寫入時將新元素插入到您的容器中。
std::copy(result.begin(), result.end(), std::back_inserter(temp));
雖然這回答了發布的問題,但您的代碼中仍然存在其他導致未定義行為的錯誤。
嘗試使用 C++ 編譯器編譯您的程序將不起作用,因為您包含了#include <bits/stdc++.h>
,它是不符合 tC++ 標准的 header。
你永遠不應該包含這個文件。
您正在使用典型的競爭性編程材料,但包括所有 C++ 頭文件並且不使用它們會無緣無故地占用編譯時間。
然后,您鍵入定義典型的競爭性編程縮寫。 其中2個,你不使用。 那么就沒有理由定義它們了。
我建議不要再這樣做了。 在 C++ 中,請使用using
語句。
然后,雖然你想快點,但你將arr
按值傳遞給你的 print function。 這將復制整個向量。
您分配/比較了很多 int 與 unsigned int 值。 這是你不應該做的。
另外:請使用有意義的變量名並寫注釋。 越多越好。
關於您的特定錯誤。 兩個std::copy
語句都使用end
迭代器作為目標。 結束就是結束。 它超過了向量的末尾。 請改用std::back_inserter
。
關於算法。 我花了一段時間才意識到你基本上想要創建二進制數。 沒有其他的。 不幸的是,您以非常復雜的方式翻譯了它。
通常,您只需從 0 數到 2^n-1,然后顯示數據。 就這樣。 因為數字可能是任意長度,我們將使用手動添加數字,就像在 scholl 中在一張紙上一樣。 很簡單。
然后一切都歸結為幾行代碼。
請參見:
#include <iostream>
#include <vector>
int main() {
// Read length of binary number to create and validate input
if (int numberOfDigits{}; (std::cin >> numberOfDigits and numberOfDigits > 0)) {
// Here we will store the binary digits, so 0s or 1s
std::vector<int> digits(numberOfDigits,0);
// Som printing helper
std::cout << '[';
bool printComma{};
// We need to print 2^n possible combinations
for (int i = 0; i < (1 << numberOfDigits); ++i) {
// Print comma, if need
if (printComma) std::cout << ','; printComma = true;
// Print all digits of the binary number
for (const int d : digits) std::cout << d;
// Calculate next binary number
int carry = 0;
for (int index=numberOfDigits -1; index >=0; --index) {
const int sum = digits[index] + ((index == (numberOfDigits - 1)?1:0)) + carry;
carry = sum / 2;
digits[index] = sum % 2;
}
}
std::cout << ']';
}
}
如果有問題,我很樂意回答。
保留不會增加向量大小。
嘗試使用 C++ 編譯器編譯您的程序將不起作用,因為您包含了#include <bits/stdc++.h>
,它是不符合 tC++ 標准的 header。
你永遠不應該包含這個文件。 絕不。
因此,您使用的是典型的競爭性編程材料,但為什么要包含所有 C++ 標頭而不使用它們。 這是浪費時間。
然后,您鍵入定義典型的無意義的競爭性編程縮寫。 其中2個,你不使用,為什么要定義它們?
不要再這樣做了。 在 C++ 中,請使用using
語句。
然后,雖然你想快點,但你將arr
按值傳遞給你的 print function。 這將復制整個向量。
您分配/比較了很多 int 與 unsigned int 值。 這是你不應該做的。
另外:使用有意義的變量名並寫注釋。 越多越好。
關於算法。 我花了一段時間才意識到你基本上想要創建二進制數。 沒有其他的。 不幸的是,您以非常復雜的方式翻譯了它。
通常,您只需從 0 數到 n-1,然后顯示數據。 就這樣。 因為數字可能是任意長度,我們將使用手動添加數字,就像在 scholl 中在一張紙上一樣。 很簡單。
然后一切都歸結為幾行代碼。
請參見:
#include <iostream>
#include <vector>
int main() {
// Read length of binary number to create and validate input
if (int numberOfDigits{}; (std::cin >> numberOfDigits and numberOfDigits > 0)) {
// Here we will store the binary digits, so 0s or 1s
std::vector<int> digits(numberOfDigits,0);
// Som printing helper
std::cout << '[';
bool printComma{};
// We need to print 2^n possible combinations
for (int i = 0; i < (1 << numberOfDigits); ++i) {
// Print comma, if need
if (printComma) std::cout << ','; printComma = true;
// Print all digits of the binary number
for (const int d : digits) std::cout << d;
// Calculate next binary number
int carry = 0;
for (int index=numberOfDigits -1; index >=0; --index) {
const int sum = digits[index] + ((index == (numberOfDigits - 1)?1:0)) + carry;
carry = sum / 2;
digits[index] = sum % 2;
}
}
std::cout << ']';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.