This is a list of operators in the C++ and C programming languages. All the operators listed exist in C++; the third column indicates whether an operator is also present in C. It should also be noted that C does not support operator overloading.
The following operators are sequence points in both languages (when not overloaded): %26amp;%26amp;, ||, ?:, and , (the comma operator).
C++ also contains the type conversion operators const_cast, static_cast, dynamic_cast, and reinterpret_cast which are not listed in the table for brevity. The formatting of these operators means that their precedence level is unimportant.
Those operators that are in C, with the exception of the comma operator, are also in Java, Perl, C#, and PHP with the same precedence, associativity, and semantics.
Tuesday, July 14, 2009
Index [] for map but not multimap, C++. ?
Why does C++ STL's map overload the operator [] but the multimap does not?
Index [] for map but not multimap, C++. ?
Because using [] would be undefined for a multi-map.
A multi-map allows for a one-to-many mapping while a map uses a one-to-one.
For a map you can use the [] operater for getting and retrieving values. But with a multi-map, you have more than one value for each key. So how would you define the [] operator in a way that would work for all cases?
There really isn't an elegant way to do it.
~X~
Index [] for map but not multimap, C++. ?
Because using [] would be undefined for a multi-map.
A multi-map allows for a one-to-many mapping while a map uses a one-to-one.
For a map you can use the [] operater for getting and retrieving values. But with a multi-map, you have more than one value for each key. So how would you define the [] operator in a way that would work for all cases?
There really isn't an elegant way to do it.
~X~
C++ help with character arrays?
I am supposed to compare two strings and write out a message that says"they are the same or the 1st string is greater. the first c-strign I can use the %26lt;%26lt;operator, but on the 2nd I have to use for loop. I worte the program,but it doesn't work correctly. Even if I enter a larger string than the 1st string it gives me that the first is larger than the second. I am also to declare an array w/20 char and initialize it w/ a string constant. then declare a 2nd array of 20 char. this is what I have.
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;cstring%26gt;
using namespace std;
int main ()
{
char a[21]= "Have a nice day!";
char cstring[21];
cout%26lt;%26lt;"Please enter a word: ";
cin.getline (cstring,21);
if (strcmp (a,cstring)%26gt;0)
{
cout%26lt;%26lt;"The first string is greater than the other.\n";
cout%26lt;%26lt;a%26lt;%26lt;" is greater than: "%26lt;%26lt;cstring%26lt;%26lt;endl;
}
for (int index=0; index%26lt;21;index++)
{
cout%26lt;%26lt;cstring[index]%26lt;%26lt;endl;
}
return 0;
}
C++ help with character arrays?
i am not an expert C++ programmer but from what I know, while using strcmp you must type the exact same string in as before but with a different number of charectors. From you assignment it sound like you need to compare any two strings. I would then use strlen instead. Heres a good example i made:
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;cstring%26gt;
using namespace std;
int main(int argc, char *argv[])
{
char cstring1[21];
char cstring2[21];
cout %26lt;%26lt; "This Program Compares 2 Strings and Tells Which is Greater in length\n";
cout %26lt;%26lt; " 1st Phrase: ";
cin.getline (cstring1,21); //Recieves input for string one
cout %26lt;%26lt; " 2nd Phrase: ";
cin.getline (cstring2,21); //Recieves input for string 2
if (strlen(cstring1) %26gt; strlen(cstring2)) //is string one larger?
{
cout %26lt;%26lt; cstring1 %26lt;%26lt; " is Greater in Length than the Sting " %26lt;%26lt; cstring2;
}else if(strlen(cstring1) == strlen(cstring2)){ //are the strings equal?
cout %26lt;%26lt; "The phrases are equal!";
}else{ //if all other fails do this!
cout %26lt;%26lt; cstring2 %26lt;%26lt; " is Greater in Length than the Sting " %26lt;%26lt; cstring1;
}
cout %26lt;%26lt; "\n\nPhrase 1\tPhrase 2\n";
for (int index=0; index%26lt;21;index++)
{
cout %26lt;%26lt; " " %26lt;%26lt; cstring1[index] %26lt;%26lt; "\t\t " %26lt;%26lt; cstring2[index] %26lt;%26lt; endl;
}
return 0;
}
------------------------Outputs-------...
This Program Compares 2 Strings and Tells Which is Greater in length
1st Phrase: string 1
2nd Phrase: string 22
string 22 is Greater in Length than the String string 1
String 1 String 2
s s
t t
r r
i i
n n
g g
1 2
2
Hope this helps ya!
Reply:When strcmp return 1 or -1 it does not say something bout the length!
Strcmp compares the characters from left to right. When a character is different (see ascii table) then the other one, it will stop processing. It returns 1 or -1 depending on the differences in ASCII value of the characters.
See the ascii table :http://www.asciitable.com/ for values of characters.
Reply:same string return 0;
larger string return -1;
shorter string return 1;
I can not see anything wrong, check my output.
----------------
int main(int argc, char *argv[])
{
char a[21]= "Have a nice day!";
char cstring[21];
cout%26lt;%26lt;"Please enter a word: ";
cin.getline (cstring,21);
cout %26lt;%26lt; "strcmp()=" %26lt;%26lt; strcmp (a,cstring) %26lt;%26lt; "\n";
if (strcmp (a,cstring)%26gt;0)
{
cout%26lt;%26lt;"The first string is greater than the other.\n";
cout%26lt;%26lt;a%26lt;%26lt;" is greater than: "%26lt;%26lt;cstring%26lt;%26lt;endl;
}
for (int index=0; index%26lt;21;index++)
{
cout%26lt;%26lt;cstring[index]%26lt;%26lt;endl;
}
return 0;
}
------------
Please enter a word: Have a nice day!
strcmp()=0
H
a
v
e
a
n
i
c
e
d
a
y
!
-----------------------
Please enter a word: Have a nice day!Have a nice day!
strcmp()=-1
H
a
v
e
a
n
i
c
e
d
a
y
!
H
a
v
e
------------------------
Please enter a word: Have a nice d
strcmp()=1
The first string is greater than the other.
Have a nice day! is greater than: Have a nice d
H
a
v
e
a
n
i
c
e
d
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;cstring%26gt;
using namespace std;
int main ()
{
char a[21]= "Have a nice day!";
char cstring[21];
cout%26lt;%26lt;"Please enter a word: ";
cin.getline (cstring,21);
if (strcmp (a,cstring)%26gt;0)
{
cout%26lt;%26lt;"The first string is greater than the other.\n";
cout%26lt;%26lt;a%26lt;%26lt;" is greater than: "%26lt;%26lt;cstring%26lt;%26lt;endl;
}
for (int index=0; index%26lt;21;index++)
{
cout%26lt;%26lt;cstring[index]%26lt;%26lt;endl;
}
return 0;
}
C++ help with character arrays?
i am not an expert C++ programmer but from what I know, while using strcmp you must type the exact same string in as before but with a different number of charectors. From you assignment it sound like you need to compare any two strings. I would then use strlen instead. Heres a good example i made:
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;cstring%26gt;
using namespace std;
int main(int argc, char *argv[])
{
char cstring1[21];
char cstring2[21];
cout %26lt;%26lt; "This Program Compares 2 Strings and Tells Which is Greater in length\n";
cout %26lt;%26lt; " 1st Phrase: ";
cin.getline (cstring1,21); //Recieves input for string one
cout %26lt;%26lt; " 2nd Phrase: ";
cin.getline (cstring2,21); //Recieves input for string 2
if (strlen(cstring1) %26gt; strlen(cstring2)) //is string one larger?
{
cout %26lt;%26lt; cstring1 %26lt;%26lt; " is Greater in Length than the Sting " %26lt;%26lt; cstring2;
}else if(strlen(cstring1) == strlen(cstring2)){ //are the strings equal?
cout %26lt;%26lt; "The phrases are equal!";
}else{ //if all other fails do this!
cout %26lt;%26lt; cstring2 %26lt;%26lt; " is Greater in Length than the Sting " %26lt;%26lt; cstring1;
}
cout %26lt;%26lt; "\n\nPhrase 1\tPhrase 2\n";
for (int index=0; index%26lt;21;index++)
{
cout %26lt;%26lt; " " %26lt;%26lt; cstring1[index] %26lt;%26lt; "\t\t " %26lt;%26lt; cstring2[index] %26lt;%26lt; endl;
}
return 0;
}
------------------------Outputs-------...
This Program Compares 2 Strings and Tells Which is Greater in length
1st Phrase: string 1
2nd Phrase: string 22
string 22 is Greater in Length than the String string 1
String 1 String 2
s s
t t
r r
i i
n n
g g
1 2
2
Hope this helps ya!
Reply:When strcmp return 1 or -1 it does not say something bout the length!
Strcmp compares the characters from left to right. When a character is different (see ascii table) then the other one, it will stop processing. It returns 1 or -1 depending on the differences in ASCII value of the characters.
See the ascii table :http://www.asciitable.com/ for values of characters.
Reply:same string return 0;
larger string return -1;
shorter string return 1;
I can not see anything wrong, check my output.
----------------
int main(int argc, char *argv[])
{
char a[21]= "Have a nice day!";
char cstring[21];
cout%26lt;%26lt;"Please enter a word: ";
cin.getline (cstring,21);
cout %26lt;%26lt; "strcmp()=" %26lt;%26lt; strcmp (a,cstring) %26lt;%26lt; "\n";
if (strcmp (a,cstring)%26gt;0)
{
cout%26lt;%26lt;"The first string is greater than the other.\n";
cout%26lt;%26lt;a%26lt;%26lt;" is greater than: "%26lt;%26lt;cstring%26lt;%26lt;endl;
}
for (int index=0; index%26lt;21;index++)
{
cout%26lt;%26lt;cstring[index]%26lt;%26lt;endl;
}
return 0;
}
------------
Please enter a word: Have a nice day!
strcmp()=0
H
a
v
e
a
n
i
c
e
d
a
y
!
-----------------------
Please enter a word: Have a nice day!Have a nice day!
strcmp()=-1
H
a
v
e
a
n
i
c
e
d
a
y
!
H
a
v
e
------------------------
Please enter a word: Have a nice d
strcmp()=1
The first string is greater than the other.
Have a nice day! is greater than: Have a nice d
H
a
v
e
a
n
i
c
e
d
C/c++ program needed..(urgent)?
Hi everybody.this is really urgent.i don't have time for tips or something like that.I NEED THE ACTUAL COMPLETE PROGRAM for tuesday..this program should capture every keyword %26amp; operator %26amp; ID %26amp; num %26amp; delimiter %26amp; ...of C program and scan them based on their state diagrams on a symbol table that contains: row,column,nest.
C/c++ program needed..(urgent)?
Get Dev-C++, it is open source!
Bloodshed.net.
I have expirience with this program, it is very good and includes everything you need!
Reply:Do not take this personally since I'm only trying to help you. I procrastinate with my homeworks but I will never do what you are doing by asking this question. First of all, your question is very vague. You should never ask for "THE ACTUAL COMPLETE PROGRAM" to one of your assignments since most of the people that can help you had to struggle when they were in your position and overcame that difficulty with patience and hard work. Second of all, if you need help then you should learn to ask good questions and provide plenty of detail. I have an idea of what your program should do and I will say that from personal experience you still have plenty of time to get it done. That is of course if you have been keeping up with work in your class. Otherwise you should at least attempt to get a part of your program done. Trust me, it is hard but if you do get your program working all by yourself you feel the satisfaction of getting it done.
floral
C/c++ program needed..(urgent)?
Get Dev-C++, it is open source!
Bloodshed.net.
I have expirience with this program, it is very good and includes everything you need!
Reply:Do not take this personally since I'm only trying to help you. I procrastinate with my homeworks but I will never do what you are doing by asking this question. First of all, your question is very vague. You should never ask for "THE ACTUAL COMPLETE PROGRAM" to one of your assignments since most of the people that can help you had to struggle when they were in your position and overcame that difficulty with patience and hard work. Second of all, if you need help then you should learn to ask good questions and provide plenty of detail. I have an idea of what your program should do and I will say that from personal experience you still have plenty of time to get it done. That is of course if you have been keeping up with work in your class. Otherwise you should at least attempt to get a part of your program done. Trust me, it is hard but if you do get your program working all by yourself you feel the satisfaction of getting it done.
floral
How to do an increment operation in C ( i++) using bitwise operators ?
Easy but pointless, therefore this must be a homework question! nevertheless here goes.. Use the XOR and SHIFT operators. Also, it is assumed that when using the XOR operator that the result is placed in the first operand, i.e, b=b XOR c
Example pseudo code adding 00000001 (1) to 10110011(179).
Variables:
a=10110011 ( Original number and will also contain final result )
b ( temp variable )
c ( temp variable )
Routine:
1) b=a
2) c=00000001
3) b XOR c
4) IF b%26gt;a, THEN a=b, EXIT
5) a=b
6) SHIFTLEFT c
7) GOTO 3)
Basically all that is happening is that working from right to left, you are removing any '1's from the original number until you find a '0' which you then change to a '1'.
NOTE: you will have to check that you are looping no more than the number of bits in the number otherwise if a==11111111, it will be reset to zero and you will end up in an infinite loop! ;o)
How to do an increment operation in C ( i++) using bitwise operators ?
You don't. The increment operator is used to increment a number in c++.
Bitwise operators are used for bitwise operations.
Reply:This sounds like a homework problem - as noone would want to do this otherwise.
You will have to implement a 2 bit addition function with carry and then repeat and shift for 32 bits, if you are using 32-bit integers.
Reply:seems like a pretty silly task. the compiler optimizes the increment operator.
you use a barrel shifter i suppose. you would only need to use the '%26lt;%26lt;' and '%26amp;'.
Example pseudo code adding 00000001 (1) to 10110011(179).
Variables:
a=10110011 ( Original number and will also contain final result )
b ( temp variable )
c ( temp variable )
Routine:
1) b=a
2) c=00000001
3) b XOR c
4) IF b%26gt;a, THEN a=b, EXIT
5) a=b
6) SHIFTLEFT c
7) GOTO 3)
Basically all that is happening is that working from right to left, you are removing any '1's from the original number until you find a '0' which you then change to a '1'.
NOTE: you will have to check that you are looping no more than the number of bits in the number otherwise if a==11111111, it will be reset to zero and you will end up in an infinite loop! ;o)
How to do an increment operation in C ( i++) using bitwise operators ?
You don't. The increment operator is used to increment a number in c++.
Bitwise operators are used for bitwise operations.
Reply:This sounds like a homework problem - as noone would want to do this otherwise.
You will have to implement a 2 bit addition function with carry and then repeat and shift for 32 bits, if you are using 32-bit integers.
Reply:seems like a pretty silly task. the compiler optimizes the increment operator.
you use a barrel shifter i suppose. you would only need to use the '%26lt;%26lt;' and '%26amp;'.
Accessing Elements of C Structure?
I'm taking a class in which most of the programming is done in C. I'm much more familiar with C++. Anyways, I am using structures in a program and from C++ I know you can access an element of a structure using the dot operater (i.e. structureVariable.element). However, someone in my class mentioned something about using -%26gt; instead of the dot operator. In my program in one C file I used the dot operator and it worked just fine (in the file a structure variable was declared). But in another file, where a function receives the structure variable like this OPTION *options the dot operator gives me an error, but when I switch to -%26gt; it works. I tried using the -%26gt; in the other file but it gave me errors. Why is this happening? When is it appropriate to use the dot operator and when is it appropriate to use -%26gt;?
Accessing Elements of C Structure?
Hi,
suppose u have a structure
struct struct_name
{
int age;
};
now u declare two structure variables
struct struct_name a,*p;
where a is a normal structure variable and *p is a structure pointer.
Now if u want to access structure variable age then write:
a.age; (If u r accessing structure element with a structure variable, use a period.
p-%26gt;age; (If u r accessing structure element with a structure pointer, use a -%26gt;.
Reply:It is pretty much the same in c and c++:
If you declare a type you normally refer its elements with a dot operator:
typedef struct student
{
int age;
int class;
char name[25];
}boy;
int main()
{
.
.
.
boy b1;
b1.age = 16;
printf("%d", b1.age);
...
}
If you are declaring a pointer to a type or any array of types you use the -%26gt; operator. Think it is called the indirection or the redirection operator...
boy *b1;
b1 = (boy) malloc(sizeof(boy));
b1-%26gt;age = 16;
printf("%d", b1-%26gt;age);
Accessing Elements of C Structure?
Hi,
suppose u have a structure
struct struct_name
{
int age;
};
now u declare two structure variables
struct struct_name a,*p;
where a is a normal structure variable and *p is a structure pointer.
Now if u want to access structure variable age then write:
a.age; (If u r accessing structure element with a structure variable, use a period.
p-%26gt;age; (If u r accessing structure element with a structure pointer, use a -%26gt;.
Reply:It is pretty much the same in c and c++:
If you declare a type you normally refer its elements with a dot operator:
typedef struct student
{
int age;
int class;
char name[25];
}boy;
int main()
{
.
.
.
boy b1;
b1.age = 16;
printf("%d", b1.age);
...
}
If you are declaring a pointer to a type or any array of types you use the -%26gt; operator. Think it is called the indirection or the redirection operator...
boy *b1;
b1 = (boy) malloc(sizeof(boy));
b1-%26gt;age = 16;
printf("%d", b1-%26gt;age);
Differentiate between c nd c++?
differentiate between c nd c++
use of ::(scope resolution operator)
what kind of projects are suitable for c and c++
what is a class? example
Differentiate between c nd c++?
O_o... this sounds like a homework question. And such I won't give you clear answers. I will give you an overview and suggest that you visit www.wikipedia.org and search for C Programming and C++ Programming.
C is a procedural language thats considered C++ predecessor.
C++ was considered the extension to C by adding Object Oriented Programming/Design to the language.
:: is used When you need to access or define a part of a class.
Reply:C++ is a superset of C. It is object oriented. It has all of the capabilities of C, plus it allows you to use polymorphism and inheritance.
This link can provide you with a lot more detail
http://unthought.net/c++/c_vs_c++.html
Reply:Do your own homework.
use of ::(scope resolution operator)
what kind of projects are suitable for c and c++
what is a class? example
Differentiate between c nd c++?
O_o... this sounds like a homework question. And such I won't give you clear answers. I will give you an overview and suggest that you visit www.wikipedia.org and search for C Programming and C++ Programming.
C is a procedural language thats considered C++ predecessor.
C++ was considered the extension to C by adding Object Oriented Programming/Design to the language.
:: is used When you need to access or define a part of a class.
Reply:C++ is a superset of C. It is object oriented. It has all of the capabilities of C, plus it allows you to use polymorphism and inheritance.
This link can provide you with a lot more detail
http://unthought.net/c++/c_vs_c++.html
Reply:Do your own homework.
Subscribe to:
Posts (Atom)