Sử dụng toán tử gán, chúng ta có thể gán giá trị cho các biến
Dấu đẳng thức (=) được dùng làm toán tử gán trong C
Ở đây, giá trị 5 đã được gán cho biến var
Ví dụ
#include int main() { int a = 10; int b = a; printf("a = %d \t b = %d\n",a,b); return 0; }
chạy nó
Ở đây, giá trị của a đã được gán cho biến b. Bây giờ, cả a và b sẽ giữ giá trị 10
Về cơ bản, giá trị của toán hạng bên phải sẽ được gán cho toán hạng bên trái
Giải thích bằng hình ảnh
Toán tử gán hợp chất
Nhà điều hành
Nghĩa
Ví dụ
(a = 10 , b = 5)
+=
L=L+R
thêm toán hạng trái và phải và gán kết quả ở bên trái
a+=b;giống như a=a+b
sau khi thực thi a sẽ giữ nguyên 15
-=
L=L-R
trừ toán hạng bên phải từ toán hạng bên trái và gán kết quả cho bên trái
a-=b;giống như a=a-b
sau khi thực hiện a sẽ giữ 5
*=
L=L*R
nhân cả toán hạng bên phải và bên trái và lưu kết quả ở bên trái
a*=b;giống như a=a*b
sau khi thực hiện a sẽ giữ 50
/=
L=L/R
chia toán hạng bên trái cho toán hạng bên phải và lưu kết quả ở bên trái
a/=b;giống như a=a/b
sau khi thực hiện a sẽ giữ 2
%=
L=L%R
Sau khi chia toán hạng sang trái và phải, phần còn lại sẽ được lưu ở bên trái
a%=b;giống như a=a%b
sau khi thực thi a sẽ giữ nguyên 0
Chương trình mẫu
Ví dụ
#include int main() { int a = 10, b = 5; a+=b; // same as a=a+b printf("value of a+b = %d\n",a); // a will hold the result return 0; }
chạy nó
Chủ đề bạn có thể thích
Cú pháp 2. Chỉ định nhiều nhất str_num ký tự của str bắt đầu bằng chỉ mục str_idx. Nó ném out_of_range của str idx > str. kích thước().
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *thisCPP
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 9
string& string::assign (const char* chars, size_type chars_len) *chars : is the pointer to the array to be assigned. chars_len : is the number of characters to be assigned from character array. Note : that chars must have at least chars_len characters. Returns : *this. 0
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 3
Original String : Hello World! After assign() : GeeksforGeeks 4
Original String : Hello World! After assign() : GeeksforGeeks 5 Original String : Hello World! After assign() : GeeksforGeeks 6 Original String : Hello World! After assign() : GeeksforGeeks 7
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 9
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 0 string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 1
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____183
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____185
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____187
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 9string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 60
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____262
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 64
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 65
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 66 string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 67
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______360______361Original String : Hello World! After assign() : GeeksforGeeks 62
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______364Original String : Hello World! After assign() : GeeksforGeeks 65Original String : Hello World! After assign() : GeeksforGeeks 62
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28Original String : Hello World! After assign() : GeeksforGeeks 69 Original String : Hello World! After assign() : GeeksforGeeks 0
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____32
Original String : Hello World! After assign() : GeeksforGeeks 8
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______35 Original String : Hello World! After assign() : GeeksforGeeks 6
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
đầu ra.
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 6cú pháp 3. Gán các ký tự của chuỗi C cstr. Nó ném ra length_error nếu kích thước kết quả vượt quá số ký tự tối đa.
Original String : Hello World! After assign() : GeeksforGeeks 6CPP
Original String : Hello World! After assign() : GeeksforGeeks 09
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 3
Original String : Hello World! After assign() : GeeksforGeeks 4
Original String : Hello World! After assign() : GeeksforGeeks 5 Original String : Hello World! After assign() : GeeksforGeeks 6 Original String : Hello World! After assign() : GeeksforGeeks 7
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 9
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 0 Original String : Hello World! After assign() : GeeksforGeeks 19
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____622
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______624____365Original String : Hello World! After assign() : GeeksforGeeks 62
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 9string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 60
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____632
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 64
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 65
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 66 string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 67
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______640Original String : Hello World! After assign() : GeeksforGeeks 61Original String : Hello World! After assign() : GeeksforGeeks 62
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28Original String : Hello World! After assign() : GeeksforGeeks 69 Original String : Hello World! After assign() : GeeksforGeeks 46
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____648
Original String : Hello World! After assign() : GeeksforGeeks 8
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______35 Original String : Hello World! After assign() : GeeksforGeeks 6
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
đầu ra.
Original String : Hello World! After assign() : GeeksforGeeksCú pháp 4. Gán các ký tự chars_len của mảng ký tự chars. Nó ném ra length_error nếu kích thước kết quả vượt quá số ký tự tối đa.
string& string::assign (const char* chars, size_type chars_len) *chars : is the pointer to the array to be assigned. chars_len : is the number of characters to be assigned from character array. Note : that chars must have at least chars_len characters. Returns : *this.CPP
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 55
Original String : Hello World! After assign() : GeeksforGeeks 56
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 3
Original String : Hello World! After assign() : GeeksforGeeks 4
Original String : Hello World! After assign() : GeeksforGeeks 5 Original String : Hello World! After assign() : GeeksforGeeks 6 Original String : Hello World! After assign() : GeeksforGeeks 7
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 9
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 0 Original String : Hello World! After assign() : GeeksforGeeks 19
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____669
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____671
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______624____365Original String : Hello World! After assign() : GeeksforGeeks 75
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 9string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 60
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____632
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 64
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 65
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 66 string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 67
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______640Original String : Hello World! After assign() : GeeksforGeeks 61Original String : Hello World! After assign() : GeeksforGeeks 62
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28Original String : Hello World! After assign() : GeeksforGeeks 69 Original String : Hello World! After assign() : GeeksforGeeks 46
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____648
Original String : Hello World! After assign() : GeeksforGeeks 8
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______35 Original String : Hello World! After assign() : GeeksforGeeks 6
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
đầu ra.
#include int main() { int a = 10, b = 5; a+=b; // same as a=a+b printf("value of a+b = %d\n",a); // a will hold the result return 0; } 8Cú pháp 5. Gán số lần xuất hiện của ký tự c. Nó ném ra length_error nếu num bằng chuỗi. npos
#include int main() { int a = 10, b = 5; a+=b; // same as a=a+b printf("value of a+b = %d\n",a); // a will hold the result return 0; } 53CPP
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 03
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 04
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 3
Original String : Hello World! After assign() : GeeksforGeeks 4
Original String : Hello World! After assign() : GeeksforGeeks 5 Original String : Hello World! After assign() : GeeksforGeeks 6 Original String : Hello World! After assign() : GeeksforGeeks 7
Original String : Hello World! After assign() : GeeksforGeeks 8
Original String : Hello World! After assign() : GeeksforGeeks 9
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 0 Original String : Hello World! After assign() : GeeksforGeeks 19
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____217
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____219
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______221____222Original String : Hello World! After assign() : GeeksforGeeks 62
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 9string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 60
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____632
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 64
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 65
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 66 string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 67
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 2
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____640string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 38Original String : Hello World! After assign() : GeeksforGeeks 62
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____28Original String : Hello World! After assign() : GeeksforGeeks 69 Original String : Hello World! After assign() : GeeksforGeeks 46
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3____648
Original String : Hello World! After assign() : GeeksforGeeks 8
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 3______35 Original String : Hello World! After assign() : GeeksforGeeks 6
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 63
đầu ra.
string& string::assign (const string& str, size_type str_idx, size_type str_num) str : is the string to be assigned. str_idx : is the index number in str. str_num : is the number of characters picked from str_idx to assign Return : *this 02Cú pháp 6. Gán tất cả các ký tự của phạm vi [beg, end). Nó ném length_error nếu phạm vi vượt quá nội dung thực tế của chuỗi.