473,766 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

count 1 in string

I have a list of strings composed of only 0 and 1, like
0000100001 1000010000, ....
They all have the same length.
Now they are stored in a vector of strings.
I want to calculate count(string1 xor string2).

for example:
1010, 0101 -xor- 1111 -- 4 (four 1s in the result string)
1111, 0000 -xor- 0000 -- 0

I can do it by string comparing, but I think it's really ugly.
And I do think there must be some easy and simple method, but I don't
know how.
Can you help me?
Jun 27 '08 #1
4 2255
On Fri, 30 May 2008 05:38:32 -0700, thomas wrote:
I have a list of strings composed of only 0 and 1, like 0000100001
1000010000, ....
They all have the same length.
Now they are stored in a vector of strings. I want to calculate
count(string1 xor string2).

for example:
1010, 0101 -xor- 1111 -- 4 (four 1s in the result string) 1111,
0000 -xor- 0000 -- 0

I can do it by string comparing, but I think it's really ugly. And I do
think there must be some easy and simple method, but I don't know how.
Can you help me?
Consider using the std::bitset container, which represents a (fixed
length) sequence of bits. It supports basic logical operations (including
xor), bit counting and lots more. It can also be converted easily to and
from a character string representation.

--
Lionel B
Jun 27 '08 #2
thomas wrote:
I have a list of strings composed of only 0 and 1, like
0000100001 1000010000, ....
They all have the same length.
Now they are stored in a vector of strings.
I want to calculate count(string1 xor string2).

for example:
1010, 0101 -xor- 1111 -- 4 (four 1s in the result string)
1111, 0000 -xor- 0000 -- 0
Uh... 1111 ^ 0000 =1111, unless you're using some funky definition of
xor.
I can do it by string comparing, but I think it's really ugly.
And I do think there must be some easy and simple method, but I don't
know how.
Can you help me?
If the strings are of the same length, you can do the accumulate of the
result of converting each char into a number (by subtracting '0', the
symbol "zero") and XORing those. Basically it'll be a loop o'er the
size of the string (using the iterators)... Simpler? Convert each into
a number and then xor those and then count the bits in the result (find
the algo on the 'net).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #3
>
Uh... *1111 ^ 0000 =1111, unless you're using some funky definition of
* xor.
that's a typo, I meant 1111 ^ 0000 -1111, :)
If the strings are of the same length, you can do the accumulate of the
result of converting each char into a number (by subtracting '0', the
symbol "zero") and XORing those. *Basically it'll be a loop o'er the
size of the string (using the iterators)... *Simpler? *Convert each into
a number and then xor those and then count the bits in the result (find
the algo on the 'net).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4

"Lionel B" <me@privacy.net a écrit dans le message de news:
g1**********@so uth.jnrs.ja.net ...
On Fri, 30 May 2008 05:38:32 -0700, thomas wrote:
>I have a list of strings composed of only 0 and 1, like 0000100001
1000010000, ....
They all have the same length.
Now they are stored in a vector of strings. I want to calculate
count(string 1 xor string2).

for example:
1010, 0101 -xor- 1111 -- 4 (four 1s in the result string) 1111,
0000 -xor- 0000 -- 0

I can do it by string comparing, but I think it's really ugly. And I do
think there must be some easy and simple method, but I don't know how.
Can you help me?

Consider using the std::bitset container, which represents a (fixed
length) sequence of bits. It supports basic logical operations (including
xor), bit counting and lots more. It can also be converted easily to and
from a character string representation.

--
Lionel B
Suppose you have
string s1("1010");
string s2("0101");

you can do:
bitset<4b1(s1);
bitset<4b2(s2);
bitset<4bxor = b1 ^ b2;
size_t count = bxor.count();

Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4679
by: Ireneus Broncel | last post by:
I have a class which reads Groups and Users from ActiveDirectory. The Problem is, that i have about 10000 rows as product. When I am trying to read the "memberOf" Objects out of this field i get allways different count of rows. If anybody knows something about this kind of problem, I would appreciate any help. Thx.
10
2861
by: Jon | last post by:
I want to count the number of instances of a certain string(delimiter) in another string. I didn't see a function to do this in the framework (if there is, please point me to it). If not, could someone let me know if the method I've used below is efficient or if there is a better way to do it, as these will be rather large strings I'm searching in. Thanks Public Shared Function CountDelimiter(ByVal strInput As String, ByVal...
6
6704
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
3
26228
by: Kuups | last post by:
Hi! I have a question regarding the count if character within a string like for example I have a string of e.g. 123#123# I would like to determine what is the code? of getting the # sign
3
2870
by: chrisperkins99 | last post by:
It seems to me that str.count is awfully slow. Is there some reason for this? Evidence: ######## str.count time test ######## import string import time import array s = string.printable * int(1e5) # 10**7 character string
24
2329
by: Derek Hart | last post by:
Is there an efficient line of code to count the number of instances of one string within another. If I have the sentence: "I want to go to the park, and then go home." It would give me a count of 2 for the word "go" Derek
7
3478
by: Matteo Rattotti | last post by:
Hi all, i've noticed a strange beaviour of string.count: in my mind this code must work in this way: str = "a_a_a_a_" howmuch = str.count("_a_") print howmuch -> 3
2
2181
by: GoCoogs | last post by:
I'm trying to count how many items are in a dynamic collection. This is the code I have so far. *** Begin Code *** Public Class Rule Private _rulevars As RuleVarsCollection Private _rulename As String Public Property Name() As String Get
22
12491
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
3
7794
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.