Arithmetic vs Logic

Hey this is just a small thought that I was having. I know that this is VERY minute but still.

Suppose you had to search a string for the occurrence of two specific characters, which among the two given below would be more efficient?

Suppose you had to search for "c" and "s" in a string full of c and s

Is this better,
1
2
3
4
5
6
7
8
9
10
str = "cscscscc"
occurances[2];
for(int i=0; i < str.length(); i++) {
   occurances[(str[i] - 'c')/16]++; 
// if character is c then (str[i] - 'c')/16 gives 0
// if character is s then (str[i] - 'c')/16 gives 1
}

cout << "Number of c: " << occurances[0] << '\n';
cout << "Number of s: " << occurances[1] << '\n';


or is this better,

1
2
3
4
5
6
7
8
9
10
11
12
str = "cscscscc"
occurances[2];

for(int i=0; i < str.length(); i++) {
   if(str[i] == 'c')
     occurance[0]++;
   else
     occurance[1]++;
}

cout << "Number of c: " << occurances[0] << '\n';
cout << "Number of s: " << occurances[1] << '\n';



Inspiration: https://codejam.withgoogle.com/2018/challenges/00000000000000cb/dashboard
Depends.
If you want to write readable code the second is better.
In the other case the distance 's' 'c' needs to be 16. Otherwise it may crash.
In terms of computing which is easier for the computer?? Just curious and hey there's are no stupid questions.

distance between s and c should be constant because s and c are part of ASCII right?.. Do some C++ compilers not follow ASCII?
Last edited on
> Do some C++ compilers not follow ASCII?

C++ guarantees that the value of the null character is zero.

C++ also guarantees that the values of the characters representing decimal digits '0' to '9' are consecutive:
for example, '7' == '6' + 1 and '4' - '0' == 0 etc.

Other than that, no guarantees are made; the specific values are locale specific.


> In terms of computing which is easier for the computer??

The second version may generate more efficient code.

I would write it this way:
1
2
occurance[0] = std::count( str.begin(), str.end(), 'c' ) ; // count of 'c'
occurance[1] = str.size() - occurance[0] ; // count of other characters (in our case 's')  
Last edited on
Oh I see JLBorges.

Where can I learn about assembly language or how the C++ compiler works? Any book? Any recommendations for learning about computers itself?

Also another question,
if you write two programs, one with vector and another with c-style array for only just storing and retrieving values, will the compiled code for both these be the same?

Compiled code meaning the machine language or whatever (I'm bad with computers so pardon me).
Both have same efficiency?

Is this correct way to declare variable length c-style string?:
1
2
3
4
	char* text = new char[1];
	
	cin >> text;
	cout << text;


By the way JLBorges where did you learn about C++ standard and what it guarantees? Or more specifically where can I learn C++ standard?
Last edited on
> Where can I learn about assembly language or how the C++ compiler works? Any book?

I suggest that learning assembly language and understanding the innards of compilers should not be your priority at this stage.


> if you write two programs, one with vector and another with c-style array for only just storing
> and retrieving values, will the compiled code for both these be the same? ... Both have same efficiency?

Assuming that the c-style array is dynamically allocated and resizeable like a vector, the generator code for both should be similar, with comparable performance.


> Is this correct way to declare variable length c-style string?:

1
2
3
int max_chars = 100 ; // for example
char* text = new char[max_chars+1] ; // +1 for the sentinel null character
std::cin >> std::setw(max_chars+1) >> text ; // limit the size of input to avoid buffer overflow 

The correct way is to use std::string instead of variable length c-style string.


> Or more specifically where can I learn C++ standard?

Learn the most important and useful parts of C++ from a good text book (or two).
For example, Stroustrup mentions this in his introductory text book:
The value of a character, such as 'a' for a, is implementation dependent (but easily
discovered, for example, cout << int('a')).


Re. the standard:
The standard is not intended to teach how to use C++. Rather, it is an international treaty – a formal, legal, and sometimes mind-numbingly detailed technical document intended primarily for people writing C++ compilers and standard library implementations.

Fortunately, there are lots of good books that do teach how to use C++!
https://isocpp.org/std/the-standard


A good online C++ reference: https://en.cppreference.com/w/
if you write two programs, one with vector and another with c-style array for only just storing and retrieving values, will the compiled code for both these be the same?
The compiler knows the location of an array's data at compile time. That isn't true of a vector. So it's possible that the compiler can generate more efficient code to access items in an array.

However, the difference is tiny and if one is worried about performance, it's likely that there are much bigger gains to be had. Performance is usually gained by choosing the right data structures and algorithms for the problem and that's where I suggest you spend your time.

Also, it's usually far more important to write code that can easily be read and understood.

Is this correct way to declare variable length c-style string?:

No. You've created space for a one-character string (plus a terminating null byte). Variable length strings must, by definition, grow and shrink. That requires a class, like std::string.
How does string work if I had to implement it manually using pointers?
Does string have a maximum amount of characters it can have?
Last edited on
How does string work if I had to implement it manually using pointers?

It's not clear if you're talking about writing your own string class, or if you mean passing a pointer to a C-style string to std::string's constructor. If you mean passing a pointer to a C-style string to std::string's constructor, std::string will make a copy of the passed string.
Does string have a maximum amount of characters it can have?

Practically no. However, you are limited by two things that you're not likely to reach.
1) The number of characters that can be represented in a size_t.
2) The amount of memory that can be allocated in your program
.
What about this one?
1
2
3
4
5
6
7
8
str = "cscscscc"
occurances[2];

for(int i=0; i < str.length(); i++)
     occurance[str[i] == 's']++;

cout << "Number of c: " << occurances[0] << '\n';
cout << "Number of s: " << occurances[1] << '\n';
Topic archived. No new replies allowed.