Matrix
2D vector of doubles
1)
void print(const vector<vector<double>>& m) {
for (unsigned row = 0; row < m.size(); row++) {
for (unsigned col = 0; col < m[row].size(); col++)
cout << std::setw(5) << m[row][col];
cout << '\n';
}
}
2)
void print(const vector<vector<double>>& m) {
for (const vector<double>& row : m) {
for (int elem : row)
cout << setw(5) << elem;
cout << '\n';
}
return sum;
}
3)
using Matrix = vector<vector<double>>;
void print(const Matrix& m) {
for (const vector<double>& row : m) { //for each row
for (int elem : row) // for each elem in a row
cout << setw(5) << elem;
cout << '\n';
}
return sum;
}
int main() {
vector<vector<int>> vec{ {1,2,3},
{4,5,6} };//2 by 3 matrix
for (vector<int>& row : vec) { // 행에 대한 값
for (int& elem : row) // 행의 elem 값
cin >> elem;
}
for (int row = 0; row < vec.size(); row++) {
for (int col = 0; col < vec[row].size(); col++) {
cout << vec[row][col] << '\t';
}
cout << endl;
}
return 0;
}
void print(int* m, int nRow, int nCol) {
for (int row = 0; row < nRow; row++) {
for (int col = 0; col < nCol; col++) {
cout << m[row][col] << '\t';
}
cout << endl;
}
}
int main() {
int matrix[2][2] = { {1,2},
{3,4} }; // 정적 2차원 배열
int nRow = 2, nCol = 2;
int** matrix2;
matrix2 = new int* [nRow];
for (int i = 0; i < nRow; i++)
matrix2[i] = new int[nCol];
matrix2[0][0] = 1; matrix2[0][1] = 2;
matrix2[1][0] = 3; matrix2[1][1] = 4;
print(matrix2, nRow, nCol);
for (int i = 0; i < nRow; i++)
delete[] matrix2[i];
delete[] matrix2;
return 0;
}
문자 Array 형태
char* ch= "abcd"
실제로는 5바이트로 저장된다.-> null문자를 만날때까지 서치하는 형태로 할수있다.
a |
b |
c |
d |
'\0' : null문자 |
bool find_char(const char* s, char ch) {
while (*s != '\0') { //'\0' == NULL
if (*s == ch)
return true;
s++;
}
return false;
}
int main() {
const char* phrase = "this is a phrase";
for (char ch = 'a'; ch < 'z'; ch++) { // ascii 코드 형태이기 때문에 사용가능
cout << ch << " is ";
if (!find_char(phrase, ch))
cout << "NOT";
cout << "in(" << phrase << ")" << endl;
}
return 0;
}
>>>
a is in(this is a phrase)
b is NOTin(this is a phrase)
c is NOTin(this is a phrase)
d is NOTin(this is a phrase)
e is in(this is a phrase)
f is NOTin(this is a phrase)
g is NOTin(this is a phrase)
h is in(this is a phrase)
i is in(this is a phrase)
j is NOTin(this is a phrase)
k is NOTin(this is a phrase)
l is NOTin(this is a phrase)
m is NOTin(this is a phrase)
n is NOTin(this is a phrase)
o is NOTin(this is a phrase)
p is in(this is a phrase)
q is NOTin(this is a phrase)
r is in(this is a phrase)
s is in(this is a phrase)
t is in(this is a phrase)
u is NOTin(this is a phrase)
v is NOTin(this is a phrase)
w is NOTin(this is a phrase)
x is NOTin(this is a phrase)
y is NOTin(this is a phrase)
해당 자료는 경희대학교 소프트웨어융합학과 배성호교수님 수업내용을 참조하였습니다.
'Code > C++' 카테고리의 다른 글
[C++] class & object/ private & public (0) | 2019.11.02 |
---|---|
[C++] (example) Vector Matrix (0) | 2019.10.19 |
[C++] Array / Dynamic array/ static array (0) | 2019.10.18 |
[C++] (example) Vector (0) | 2019.10.17 |
[C++] (example) & operator / pointer / pass by address/ function pointer (0) | 2019.10.17 |