Блог пользователя 000golabi

Автор 000golabi, 13 лет назад, По-английски

1- every variable declared outside of functions are static and have the default value of zero(0) .

int dp[100];
bool vis[100];
struct point { int x,y; }; 
point ps[100];
int main(){
   // all elements of dp are zero by default
   // all elements of vis are false
   // all points have x == 0 and y == 0
}

2- , is an operator and returns its rightmost statement value , for example if A and B be statements (A,B) == B .

if,for,while without a block { } take one statement so you can simply write multiple statements separated bye ,

int main() {
    int a = 1, b = 2;
    int c = a,b; // c == 2
    int A[100];
    int sumA = 0;
    for(int i = 0; i < 100; ++i)
        cin >> A[i],
        sumA += A[i];
}

3- if you declare an array inside a function the value of its elements are garbage , but if you write TYPE var[LEN] = { }; all elements will be zero by default.

int main(){
   int A[100] ; // elements of A have garbage values
   int B[100] = { } ; // all elements of B are zero 
}

4- memset is defined in <string.h> and sets the bye values of a block of memory . you can use memset to initialize all elements of an integer array to 0 or -1 but not the other values .

int main(){
   int A[100];
   memset(A,0,sizeof(A)); // all elements of A are zero
   memset(A,-1,sizeof(A)); // all elements of A are -1

   //NOT WORKING
   memset(A,other_numbers,sizeof(A)); // WRONG
}

help me update this post with other useful facts . have fun !

  • Проголосовать: нравится
  • +65
  • Проголосовать: не нравится

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +28 Проголосовать: не нравится

Actually, you can memset correctly with 256 different values, for example, 0x3F3F3F3F.

»
13 лет назад, скрыть # |
 
Проголосовать: нравится -20 Проголосовать: не нравится

насколько я помню, в данном случае

for(int i = 0; i < 100; ++i)
        cin >> A[i],
        sumA += A[i];

порядок выполнения операций, разделенных запятой, вообще говоря, не определен, и может возникнуть очевидный фэйл. насколько я прав?

»
13 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I didn't know about this one:

    for(int i = 0; i < 100; ++i)
        cin >> A[i],
        sumA += A[i];

I tried in java but doesn't work.

»
13 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +36 Проголосовать: не нравится

I use fill() to initialize elements to any values. It's defined in <algorithm>.

int main() {
   int A[100];
   fill(A, A + 100, 7); // A[i] = 7, 0 <= i <= 99
}
»
13 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

About memset: 0 and -1 work normal because memset fills bytes with a value, and 0 is "00…000", (-1) is "11…111" in binary format, so all bytes in (usually) int are filled like in a single char. So, if you use one-byte types (like [unsigned] char) you can use memset for any value (once I used it trying to avoid TL as std::fill works a little slower).

»
13 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

About int B[100] = {}; You can also initialize like this arrays and STL containers to the custom sequence of values. This is true for C++11.

int A[3] = {1, 2, 3}; 
int B[5] = {2, 1};    // this will initialize all other elements to 0, so B is {2, 1, 0, 0, 0}
vector <int> v {1, 2, 3, 4, 5};
map <string, int> M { {"bbb",1}, {"aaa",2} };  // creates a map like M["aaa"] = 2, M["bbb"] = 1
»
13 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Arrays not initialized to zeroes, memset, various other stuff... what about vector<>s? You can resize them at initialization...

*insert trollface here*

  • »
    »
    13 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Yes, you can. vector<>s are initialized with default values (which is zero for embedded types), but it's done with something like fill, so memset is definetely not slower (I think it should be even faster, because it can set memory by very big blocks using CPU vector operations instead of just int-by-int filling).

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

The 1st, 2nd, 4th tips are very basic. But this is the first time I see the third tip.

»
13 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

fill form include string.h is more faster than memset and you can fill any value you want into arrays , vectors , etc . Examples :

int main(){
	int a[1000] ;

	fill(a , a+1000 , 2000000000 ) ; // fill 2000000000 into array a[]

	int b[139][201] ;

	fill(&b[0][0] , &b[0][0] + 139 * 201 , 2000000000 ) ; // fill 2000000000 into array b[][]

	vector<int> v (1000);

	fill(v.begin() , v.end() , 2000000000 ) ; // fill 2000000000 into vector<int> v

	int c[17][32][54] ;

	fill(&c[0][0][0] , &c[0][0][0] + 17 * 32 * 54 , 2000000000 ) ; // fill 2000000000 into array c[][][]	
}