2017-06-21 139 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',

相关问题