我已經制作了一個程序,其中保留了一個隨機二進制碼字符串(例如1001101),我希望能夠創建列表或向量其中它告訴我1或0的位置。例如,1的位置列表將是{1,4,5,7}。我還想知道如何做相反的事情。例如,0的位置列表可以是{6,3,2}。我沒有要顯示的代碼,因爲我真的無法弄清楚。我在這裏找不到任何能幫助我的東西。謝謝!想要找到1或0在二進制碼字中出現的位置
回答
我認爲這可能對你有所幫助。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
string binary_string;
cin >> binary_string;
vector <int> position_of_ones,position_of_zeroes;
for(int i = 0; i < binary_string.length(); i++){
if(binary_string[i] == '0'){
position_of_zeroes.push_back(i+1);
}
else{
position_of_ones.push_back(i+1);
}
}
cout << "Position of 0s" << endl;
for(int i = 0; i < position_of_zeroes.size(); i++){
if(i != 0) cout << ",";
cout << position_of_zeroes[i];
}
cout << endl;
cout << "Position of 1s" << endl;
for(int i = 0; i < position_of_ones.size(); i++){
if(i != 0) cout << ",";
cout << position_of_ones[i];
}
cout << endl;
}
現在你可以使用這些向量。
這可能是一個愚蠢的問題,但你如何獲得載體然後打印出來?因爲我把你的代碼放到我的程序中,但沒有輸出。謝謝! – kaitbrymy
那裏。我更新了它。現在代碼也打印輸出 –
http://www.cplusplus.com/reference/vector/vector/ 您可以訪問此鏈接學習矢量 –
您可以使用二進制和&
輕鬆測試是否設置了特定位。
例如,爲了測試是否第3位在foo
設置,你可以做
bool is_set = foo & 0b100;
然後is_set
將true
如果第三位被設置或以其他方式false
。
將函數換成一個函數,你可以傳遞一個整數和你感興趣的位數,然後讓布爾回來說它是否被設置是微不足道的。
使用這樣的函數,應該很容易建立你所有集合和所有未設置位的列表。
如果您的代碼字是一個字符串,您可以a)首先將其轉換爲整數,然後按上述方法進行操作;或者b)僅迭代字符串,並將每個字符對'0'或'1'進行測試並根據結果add將當前位置設置爲正確。
根據OP,二進制碼字是一串1和零。 – ForceBru
@ForceBru答案更新。謝謝。 –
你可以做這樣的事情:Link
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
/*
*Find all positions of the a SubString in given String
*/
void findAllOccurances(std::vector<size_t> & vec, std::string data, std::string toSearch)
{
// Get the first occurrence
size_t pos = data.find(toSearch);
// Repeat till end is reached
while(pos != std::string::npos)
{
// Add position to the vector
vec.push_back(pos+1); //Added 1, we start to count positions at 1 instead of 0
// Get the next occurrence from the current position
pos =data.find(toSearch, pos + toSearch.size());
}
}
int main()
{
std::string data = "1001101";
std::vector<size_t> vec;
// Get All occurrences of the '1' in the vector 'vec'
findAllOccurances(vec, data , "1");
std::cout<<"All Index Position of '1' in given string are,"<<std::endl;
for(size_t pos : vec)
std::cout<<pos<<std::endl;
std::vector<size_t> vec0;
// Get All occurrences of the '0' in the vector 'vec0' backwards
findAllOccurances(vec0, data , "0");
std::cout<<"All Index Position of '0' in given string backwards are,"<<std::endl;
std::reverse_copy(vec0.begin(), vec0.end(), std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
輸出:
排序的 '1' 的位置在給定的字符串是, 所有指數位置在給定的字符串向後是 '0',
- 1. 如何在某個位置設置一個二進制數字1或者0
- 2. 檢查一個二進制數是否在特定位置有'0'或'1'
- 3. 如何在二進制中更改1到0和0到1(Python)
- 4. 在二進制向量中查找1前面有0前綴
- 5. 二進制補碼0到1,1,0到
- 6. 的Java:更改二進制數位的0比1和1比0的
- 7. 轉換二進制到十進制和翻轉0和1 JAVA
- 8. WebDriverException:消息:預期瀏覽器的二進制位置,但無法找到在默認位置二進制
- 9. 查找1或0的連續位串
- 10. 如何使用字節數組轉換爲二進制圖像(0位,1位)?
- 11. 將1和0的32位二進制字符串轉換爲SQL Server中的帶符號十進制數字
- 12. 爲什麼在二進制補碼(-1 >> 1)== -1而不是0?
- 13. C++找到二進制數的最重要的位?
- 14. 陣列實爲二進制(0/1)
- 15. 替換二進制形式0-> 1和1-> 0值 - perl
- 16. 查找二進制樹O(1)中的位數
- 17. SSIS僅在start_execution上才找到「二進制代碼或腳本」
- 18. 什麼glmnet二進制數據模型(P = 1或P = 0)
- 19. VERILOG:如何找到一個5位數的二進制補碼
- 20. 4位十進制到16位二進制轉換代碼
- 21. Python:1和0的字符串 - >二進制文件
- 22. 將1和0的字符串轉換爲二進制文件
- 23. 在二進制中設置二進制補碼
- 24. 在GCC調試二進制文件中的源代碼位置
- 25. 16位二進制補碼
- 26. 8位二進制代碼
- 27. 在java中設置二進制輸出的精確位數
- 28. 在C++中移位二進制數字
- 29. 正則表達式,找到數字,0或十進制
- 30. C到二進制:putchar('0'+((x >> i))& 1));
以字符串形式的二進制碼字?或整數形式? –
二進制碼字爲字符串形式 – kaitbrymy
使用for循環或while循環遍歷字符串,並將其作爲向量,一個存儲1的索引,另一個存儲0的索引 –