## Assuming that you are not implementing a portion of the standard library, which of these are legal identifiers. (This means that they follow the syntax rules for C++)
- switch
- _int
- string
what is L-Value, R-Value or Non-Modifiable L-Value. ???
In C++ and many other programming languages, the terms "L-value" and "R-value" are used to classify expressions based on whether they can appear on the left side of an assignment (`=`) operator or not. Additionally, there's a concept called "non-modifiable L-value," which refers to an L-value that cannot be modified. Here's an explanation of these terms:
1. **L-Value (Left Value)**:
- **Definition**: An L-value is an expression that refers to a memory location, and it can appear on the left side of an assignment operator (`=`). In other words, it represents an object or variable that can be modified.
- **Example**: Variables, array elements, and non-const references are typically L-values. For instance, `int x = 5;` - here, `x` is an L-value.
2. **R-Value (Right Value)**:
- **Definition**: An R-value is an expression that represents a value and cannot appear on the left side of an assignment operator. R-values are typically temporary and may not have a memory location.
- **Example**: Literal values (`5`, `"hello"`), the result of an expression (`x + y`), and function return values are often R-values.
3. **Non-Modifiable L-Value**:
- **Definition**: A non-modifiable L-value is an expression that refers to a memory location like an L-value but cannot be modified. It represents an object that is marked as const.
- **Example**: A const variable (`const int y = 10;`) is a non-modifiable L-value because you cannot modify its value.
Here are some examples to illustrate these concepts:
```cpp
int x = 5; // 'x' is an L-value.
int y = x + 3; // 'x + 3' is an R-value (temporary result).
const int z = 7; // 'z' is a non-modifiable L-value (const variable).
x = 10; // 'x' is an L-value, and it can be modified.
// 'y = 15;' would be a compilation error because 'y' is an R-value.
// 'z = 8;' would be a compilation error because 'z' is a non-modifiable L-value.
```
In summary, L-values are expressions that can appear on the left side of an assignment, R-values are expressions that represent values and cannot be on the left side of an assignment, and non-modifiable L-values are L-values that are marked as const and cannot be modified.
1. **๋ฌธ์์ด ๊ธธ์ด() ๋ฉค๋ฒ ํจ์์์ ๋ฐํ๋ฉ๋๋ค:** ์๋์, ๋ฌธ์์ด ํด๋์ค์ `length()` ๋ฉค๋ฒ ํจ์๋ `string::size_type`์ ๋ฐํํ์ง ์์ต๋๋ค. ์ผ๋ฐ์ ์ผ๋ก `size_t`๋ฅผ ๋ฐํํฉ๋๋ค.
2. **ํด๋น ํ์์ ๋ณ์๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค:** ์, `string::size_type` ํ์์ ๋ณ์๋ฅผ ์์ฑํ์ฌ ๋ฌธ์์ด๊ณผ ๊ด๋ จ๋ ํฌ๊ธฐ ๋ฐ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ ์ ์์ต๋๋ค.
3. **`size_t`์ ๊ฐ์ต๋๋ค:** ์ผ๋ฐ์ ์ผ๋ก `string::size_type`๊ณผ `size_t`๋ ๋์ผํ๊ฑฐ๋ ๋งค์ฐ ์ ์ฌํ ํ์์ ๋๋ค. ๊ทธ๋ฌ๋ ๋ ํ์์ด ํญ์ ๋์ผํ๋ค๊ณ ๋ณด์ฅ๋์ง๋ ์์ต๋๋ค.
4. **unsigned ์ ์ ํ์์ด๋ฉฐ ์ด๋ค ํฌ๊ธฐ์ ํ์์ ๋๋ค:** ๋ง์ต๋๋ค, `string::size_type`์ ๋ณดํต ์ด๋ค ํฌ๊ธฐ์ unsigned ์ ์ ํ์์ ๋๋ค.
5. **๋ฌธ์์ด size() ๋ฉค๋ฒ ํจ์์์ ๋ฐํ๋ฉ๋๋ค:** ๋ง์ต๋๋ค, ๋ฌธ์์ด ํด๋์ค์ `size()` ๋ฉค๋ฒ ํจ์๋ `string::size_type`์ ๋ฐํํฉ๋๋ค.
'เซฎโหถแต แต แตหถโแโก > coding' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] STL์ ๋ฐ๋ณต์ (Iterators) (0) | 2024.03.28 |
---|---|
[C++] STL์ด๋? ( STL์ ์ ์์ ๊ตฌ์ฑ์์) (2) | 2024.03.27 |
[C++] Introduction to C++ Syntax & Variables (0) | 2023.09.22 |
[c++] idioms (0) | 2023.09.22 |
function์ ๊ตฌ์กฐ ์ด์ผ๊ธฐ (0) | 2023.09.17 |