2017-06-21 137 views
-1

我已經制作了一個程序,其中保留了一個隨機二進制碼字符串(例如1001101),我希望能夠創建列表或向量其中它告訴我1或0的位置。例如,1的位置列表將是{1,4,5,7}。我還想知道如何做相反的事情。例如,0的位置列表可以是{6,3,2}。我沒有要顯示的代碼,因爲我真的無法弄清楚。我在這裏找不到任何能幫助我的東西。謝謝!想要找到1或0在二進制碼字中出現的位置

+0

以字符串形式的二進制碼字?或整數形式? –

+0

二進制碼字爲字符串形式 – kaitbrymy

+0

使用for循環或while循環遍歷字符串,並將其作爲向量,一個存儲1的索引,另一個存儲0的索引 –

回答

-1

我認爲這可能對你有所幫助。

#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; 
} 

現在你可以使用這些向量。

+0

這可能是一個愚蠢的問題,但你如何獲得載體然後打印出來?因爲我把你的代碼放到我的程序中,但沒有輸出。謝謝! – kaitbrymy

+0

那裏。我更新了它。現在代碼也打印輸出 –

+0

http://www.cplusplus.com/reference/vector/vector/ 您可以訪問此鏈接學習矢量 –

0

您可以使用二進制和&輕鬆測試是否設置了特定位。

例如,爲了測試是否第3位在foo設置,你可以做

bool is_set = foo & 0b100; 

然後is_settrue如果第三位被設置或以其他方式false

將函數換成一個函數,你可以傳遞一個整數和你感興趣的位數,然後讓布爾回來說它是否被設置是微不足道的。

使用這樣的函數,應該很容易建立你所有集合和所有未設置位的列表。

如果您的代碼字是一個字符串,您可以a)首先將其轉換爲整數,然後按上述方法進行操作;或者b)僅迭代字符串,並將每個字符對'0'或'1'進行測試並根據結果add將當前位置設置爲正確。

+0

根據OP,二進制碼字是一串1和零。 – ForceBru

+0

@ForceBru答案更新。謝謝。 –

0

你可以做這樣的事情: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; 
} 

Live sample!

輸出:

排序的 '1' 的位置在給定的字符串是, 所有指數位置在給定的字符串向後是 '0',

相關問題