<!DOCTYPE html>
    <html lang="vi" xmlns="https://www.w3.org/1999/xhtml" prefix="og: https://ogp.me/ns#">
    <head>
<title>Tài liệu C++ và CodeBlocks V2</title>
<meta name="description" content="Tài liệu C++ và CodeBlocks V2 - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;365.io.vn&#x002F;savefile&#x002F;tai-lieu-hoc-lap-trinh&#x002F;tai-lieu-c-va-codeblocks-v2-240.html">
<meta name="author" content="DATAONLINE.IO.VN">
<meta name="copyright" content="DATAONLINE.IO.VN [support@dataonline.io.vn]">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Tài liệu C++ và CodeBlocks V2">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;365.io.vn&#x002F;savefile&#x002F;tai-lieu-hoc-lap-trinh&#x002F;tai-lieu-c-va-codeblocks-v2-240.html">
<meta property="og:site_name" content="DATAONLINE.IO.VN">
<meta property="og:url" content="https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v2-240.html">
<link rel="shortcut icon" href="https://dataonline.io.vn/favicon.ico">
<link rel="canonical" href="https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v2-240.html">
<link rel="alternate" href="https://dataonline.io.vn/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tong-hop/" title="Tin Tức - Tổng hợp" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/gdpt/" title="Tin Tức - GDPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cac-van-ban/" title="Tin Tức - Các văn bản" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tieu-hoc/" title="Tin Tức - Tiểu học" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-3/" title="Tin Tức - Lớp 3" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-4/" title="Tin Tức - Lớp 4" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-5/" title="Tin Tức - Lớp 5" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/sgk-tieu-hoc/" title="Tin Tức - SGK Tiểu học" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-3/" title="Tin Tức - Tin học 3" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-4/" title="Tin Tức - Tin học 4" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-5/" title="Tin Tức - Tin học 5" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/thcs/" title="Tin Tức - THCS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-6-75/" title="Tin Tức - Lớp 6" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-7-75/" title="Tin Tức - Lớp 7" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-8-75/" title="Tin Tức - Lớp 8" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-9-75/" title="Tin Tức - Lớp 9" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/sgk-thcs/" title="Tin Tức - SGK THCS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-6/" title="Tin Tức - Tin học 6" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-7/" title="Tin Tức - Tin học 7" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-8/" title="Tin Tức - Tin học 8" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-9/" title="Tin Tức - Tin học 9" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/thpt/" title="Tin Tức - THPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-10/" title="Tin Tức - Lớp 10" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-11/" title="Tin Tức - Lớp 11" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lop-12/" title="Tin Tức - Lớp 12" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/sgk-thpt/" title="Tin Tức - SGK THPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-10/" title="Tin Tức - Tin học 10" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-11/" title="Tin Tức - Tin học 11" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tin-hoc-12/" title="Tin Tức - Tin học 12" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cntt/" title="Tin Tức - CNTT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/phan-mem-quan-li-va-dieu-hanh/" title="Tin Tức - Phần mềm quản lí và điều hành" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/phan-mem-ung-dung/" title="Tin Tức - Phần mềm ứng dụng" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/ung-dung-ai/" title="Tin Tức - Ứng dụng AI" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/mang-may-tinh/" title="Tin Tức - Mạng máy tính" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh/" title="Tin Tức - Lập trình" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cau-truc-thi-hsg-va-ts-vao-10/" title="Tin Tức - Cấu trúc thi HSG và TS vào 10" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/tai-lieu-hoc-lap-trinh/" title="Tin Tức - Tài liệu học lập trình" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/phan-mem-lap-trinh/" title="Tin Tức - Phần mềm lập trình" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh-truc-quan/" title="Tin Tức - Lập trình trực quan" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh-huong-thu-tuc-pop/" title="Tin Tức - Lập trình hướng thủ tục &#40;POP&#41;" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-hsg-tin-hoc-thcs/" title="Tin Tức - Đề HSG Tin học THCS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-tin-hoc-ts-vao-10-thpt-chuyen/" title="Tin Tức - Đề Tin học TS vào 10 THPT chuyên" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/de-tin-hoc-hsg-thpt/" title="Tin Tức - Đề Tin học HSG THPT" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/cham-thi/" title="Tin Tức - Chấm thi" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/themis/" title="Tin Tức - Themis" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/vnoi/" title="Tin Tức - VNOI" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/danh-sach-hoc-sinh/" title="Tin Tức - Danh sách học sinh" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/lap-trinh-huong-doi-tuong-oop/" title="Tin Tức - Lập trình hướng đối tượng &#40;OOP&#41;" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/web-nguon-mo/" title="Tin Tức - Web nguồn mở" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/he-dieu-hanh/" title="Tin Tức - Hệ điều hành" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/microsoft-windows/" title="Tin Tức - Microsoft Windows" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/ho-linux/" title="Tin Tức - Họ Linux" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/may-chu/" title="Tin Tức - Máy chủ" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/co-so-du-lieu/" title="Tin Tức - Cơ sở dữ liệu" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/web-server/" title="Tin Tức - Web Server" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/hosting-domain-vps/" title="Tin Tức - Hosting-domain-VPS" type="application/rss+xml">
<link rel="alternate" href="https://dataonline.io.vn/rss/ban-tin/" title="Tin Tức - Bản tin" type="application/rss+xml">
<link rel="preload" as="style" href="https://dataonline.io.vn/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://dataonline.io.vn/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://dataonline.io.vn/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://dataonline.io.vn/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/style.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://dataonline.io.vn/themes/default/css/news.css">
<link rel="stylesheet" href="https://dataonline.io.vn/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">DATAONLINE.IO.VN</h2>
		<p class="pull-right"><a title="DATAONLINE.IO.VN" href="https://dataonline.io.vn/">https://dataonline.io.vn</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Tài liệu C++ và CodeBlocks V2</h1>
		<ul class="list-inline">
			<li>Thứ ba - 22/11/2022 23:33</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
			<p><strong>Điều chỉnh và bổ sung tài liệu của năm học 2020-2021 (V1)</strong><br />Tài liệu C++ và CodeBlocks gồm tài liệu C++ căn bản, hướng dẫn cài và sử dụng&nbsp;CodeBlocks để thực hành C++. Dành cho học sinh THCS thay thế ngôn ngữ lập trình Pascal.<br />Tài liệu luôn được cập nhật, điều chỉnh và bổ sung phù hợp với những thay đổi của môi trường lập trình C++&nbsp;CodeBlocks.<br />Là tài liệu cơ bản dành cho học sinh THCS<br />&nbsp;</p>
		</div>
				<div class="imghome">
			<img alt="Tài liệu C++ và CodeBlocks V2" src="https://dataonline.io.vn/uploads/news/codeblock2.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p><strong>I. Hướng dẫn cài và sử dụng CodeBlocks để thực hành c++.&nbsp;</strong><br /><a href="https://365.io.vn/c/tai-lieu-c-va-codeblocks-2020-2021-14.html">Click vào đây để xem</a><br />II. Tài liệu C ++<br />1. <a href="https://dataonline.io.vn/#CD1CT">Chủ đề 1. Làm quen với C++</a>.<br /><a href="https://dataonline.io.vn/#CD1CT_11">1.1. Các phép toán số học và một số hàm toán học.</a><br /><a href="https://dataonline.io.vn/#CD1CT_12">1.2. Xuất dữ liệu mặc định và căn lề&nbsp;&nbsp;ra màn hình.</a><br /><a href="https://dataonline.io.vn/#CD1CT_121">1.2.1. Xuất mặc định.</a><br /><a href="https://dataonline.io.vn/#CD1CT_122">1.2.2. Xuất căn lề.</a><br /><a href="https://dataonline.io.vn/#CD1CT_123">1.2.3. Số chữ số thập phân.</a><br /><a href="https://dataonline.io.vn/#CD1.2.4_CT">1.2.4. Chương trình con-Hàm.</a><br /><a href="https://dataonline.io.vn/#CD2CT2">2. Chủ đề 2. Kiểu dữ liệu và&nbsp;hằng, biến.</a><br /><a href="https://dataonline.io.vn/#CD2CT21">2.1. Một số kiểu dữ liệu cơ bản thường dùng trong C++</a><br /><a href="https://dataonline.io.vn/#CD2CT22">2.2. Cách khai báo và sử dụng biến hằng, phép gán.</a><br /><a href="https://dataonline.io.vn/#CD2CT23">2.3. Nhập dữ liệu từ bàn phím, từ tệp (đọc tệp) , xuất tệp (ghi tệp).</a><br /><a href="https://dataonline.io.vn/#CD2CT24">2.4. Củng cố và luyện tập lại các phép toán số học.&nbsp;</a><br /><a href="https://dataonline.io.vn/#CD3CT3">3. Chủ đề 3. Phép so sánh, phép toán&nbsp;logic và câu&nbsp;lệnh điều kiện, lệnh lặp.</a><br /><a href="https://dataonline.io.vn/#CD3CT31">3.1. Các phép so sánh và phép toán logic&nbsp;trong C++</a><br /><a href="https://dataonline.io.vn/#CD3CT32">3.2. Câu lệnh điều kiện if, switch case ...</a><br /><a href="https://dataonline.io.vn/#CD3CT33">3.3. Câu lệnh lặp for, while, do - while.</a><br /><a href="https://dataonline.io.vn/#CD4CT">4. Chủ đề 4. Mảng một chiều (số nguyên, số thực).</a><br /><a href="https://dataonline.io.vn/#CD4CT41">4.1. Nhập, xuất mảng.</a><br /><a href="https://dataonline.io.vn/#CD4CT42">4.2. Sắp xếp tăng giảm</a> (tự code sắp xếp, sử dụng hàm có sẵn - ưu tiên sử dụng hàm sort có sẵn).<br /><a href="https://dataonline.io.vn/#CD4CT43">4.3. Kiểm tra xem giá trị X có xuất hiện trong mảng hay không? Xuất hiện bao nhiêu lần và các vị trí xuất hiện (nếu có)?</a><br /><a href="https://dataonline.io.vn/#CD4CT44">4.4. Chèn thêm phần tử X vào mảng (tại vị trí nào đó hoặc mảng đã sắp xếp sao cho vẫn đảm bảo thứ tự sắp xếp).</a><br /><a href="https://dataonline.io.vn/#CD4CT45">4.5. Xóa phần tử của mảng (tại vị trí nào đó, xóa các phần tử trùng lặp - sau khi xóa, mỗi phần tử trong mảng là duy nhất).</a><br /><a href="https://dataonline.io.vn/#CD4CT46">4.6. Kiểm tra xem có phải là mảng đối xứng không?</a><br /><a href="https://dataonline.io.vn/#CD4CT47">4.7. Tìm ƯCLN của mọi phần tử trong mảng.</a><br /><a href="https://dataonline.io.vn/#CD4CT48">4.8. Tìm BCNN của mọi phần tử trong mảng.</a><br /><a href="https://dataonline.io.vn/#CT_CD5">5. Chủ đề 5. Xâu kí tự.</a><br /><a href="https://dataonline.io.vn/#CT_CD51">5.1. Nhập, xuất xâu.</a><br /><a href="https://dataonline.io.vn/#CT_CD52">5.2. Các thao tác cơ bản với xâu.</a><br /><a href="https://dataonline.io.vn/#CT_CD53">5.3. Chuyển đổi số kiểu số sang xâu và ngược lại.</a><br /><a href="https://dataonline.io.vn/#CT_CD54">5.4. Kiểm tra xem một kí tự trong xâu có phài là chữ số hay không?</a><br /><a href="https://dataonline.io.vn/#CT_CD55">5.5. Chuyển đổi chữ HOA - chữ thường.</a><br /><a href="https://dataonline.io.vn/#CT_CD56">5.6. Chuẩn hóa xâu.</a><br /><a href="https://dataonline.io.vn/#CT_CD561">5.6.1. Xóa kí tự dấu cách thừa trong xâu.</a><br /><a href="https://dataonline.io.vn/#CT_CD562">5.6.2. Chuẩn hóa danh từ riêng.</a><br /><a href="https://dataonline.io.vn/#ND_563"><strong>5.6.3. Sắp xếp các kí tự trong xâu tăng/giảm bằng hàm sort&nbsp;</strong></a><br /><a href="https://dataonline.io.vn/#CT_CD7">6. Vector.</a><br /><br />NỘI DUNG TÀI LIỆU CHI TIẾT.</p><h2><strong>II. Tài liệu C ++</strong><br /><strong>1. </strong><a href="https://dataonline.io.vn/#MLCD1"><strong>Chủ đề 1. Làm quen với C++</strong></a><strong>.</strong></h2><p>Mục tiêu:<br />- Các phép toán với dữ liệu kiểu số cơ bản (Toán tử số học): Cộng, trừ, nhân, chia, chia lấy phần dư, chia lấy phần nguyên.<br />- Xuất ra kết quả gồm mặc định và có định dạng (số nguyên, số thực).<br />Kiến thức:<br /><a href="https://dataonline.io.vn/#MLCD1_11">1.1. Các phép toán số học và một số hàm toán học trong C++ (Toán tử số học)</a><br />* Các phép toán số học.</p><figure class="table"><table><tbody><tr><td>TT</td><td>Phép toán</td><td>Kí hiệ</td><td>Ví dụ</td><td>Kết quả</td></tr><tr><td>1</td><td>Cộng</td><td>+</td><td>9+4</td><td>13</td></tr><tr><td>2</td><td>Trừ</td><td>-</td><td>9-4</td><td>5</td></tr><tr><td>3</td><td>Nhân</td><td>*</td><td>9*4</td><td>36</td></tr><tr><td colspan="1" rowspan="3">4</td><td colspan="1" rowspan="3">Chia</td><td colspan="1" rowspan="3">/</td><td>9/4</td><td>2</td></tr><tr><td>9.0/4</td><td>2.25</td></tr><tr><td>9/4.0</td><td>2.25</td></tr><tr><td>5</td><td>Chia lấy phần dư</td><td>%</td><td>9%4</td><td>1</td></tr><tr><td>6</td><td>Tăng 1 đơn vị</td><td>++</td><td>a++</td><td>Giống a=a+1</td></tr><tr><td>7</td><td>Giảm 1 đpn vị</td><td>--</td><td>a--</td><td>Giống a=a-1</td></tr></tbody></table></figure><p>* Một số hàm toán học</p><figure class="table"><table><tbody><tr><td>TT</td><td>Tên hàm</td><td>Ý nghĩa</td><td>Thư viên</td></tr><tr><td>1</td><td>max(a,b)</td><td>Trả về giá trị lớn nhất trong hai số a, b</td><td>&nbsp;</td></tr><tr><td>2</td><td>min(a,b)</td><td>Trả về giá trị nhỏ nhất trong hai số a, b</td><td>&nbsp;</td></tr><tr><td>3</td><td>pow(x,y)</td><td>Trả về x mũ y.</td><td>#include &lt;math.h&gt;</td></tr><tr><td>4</td><td>sqrt(x)</td><td>Trả về căn bậc 2 của x.</td><td>#include &lt;math.h&gt;</td></tr><tr><td>5</td><td>cbrt(x)</td><td>Trả về căn bậc 3 của x.</td><td>#include &lt;math.h&gt;</td></tr><tr><td>6</td><td>abs(x)</td><td>&nbsp;trị tuyệt đối của số nguyên x</td><td>#include &lt;math.h&gt;</td></tr><tr><td>7</td><td>fabs(x)</td><td>trị tuyệt đối của số thực x</td><td>#include &lt;math.h&gt;</td></tr><tr><td>8</td><td>hypot(x,y)</td><td>Trả về độ dài cạnh huyền của tam giác vông có 2 cạnh góc vuông là x và y.</td><td>#include &lt;math.h&gt;</td></tr><tr><td>9</td><td>&nbsp;__gcd(a,b)</td><td>Trả về UCLN của hai số a và b</td><td>#include &lt;algorithm&gt;</td></tr><tr><td>10</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></tbody></table></figure><p><br /><a href="https://dataonline.io.vn/#MLCD1_12">1.2. Xuất dữ liệu mặc định và căn lề ra màn hình</a>.<br /><a href="https://dataonline.io.vn/#MLCD1_121">1.2.1. Xuất mặc định</a>.<br />cout&lt;&lt;Dữ liệu xuất;<br />cout&lt;&lt;Dữ liệu xuất&lt;&lt;endl;<br />Ví dụ 1:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    cout &lt;&lt; &quot;9+4 = &quot;&lt;&lt;9+4 &lt;&lt; endl;    cout &lt;&lt; &quot;9-4 = &quot;&lt;&lt;9-4 &lt;&lt; endl;    cout &lt;&lt; &quot;9*4 = &quot;&lt;&lt;9*4 &lt;&lt; endl;    cout &lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl;    cout &lt;&lt; &quot;9%4 = &quot;&lt;&lt;9%4 &lt;&lt; endl;    cout &lt;&lt; &quot;9/4 = &quot;&lt;&lt;9/4 &lt;&lt; endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/c2hZUW9.png" alt="" /><br />Kết quả:<br /><strong><img src="https://i.imgur.com/q4AdIEd.jpg" alt="" /></strong><br /><a href="https://dataonline.io.vn/#MLCD1_122">1.2.2. Xuất căn lề.</a><br />cout&lt;&lt;setw(n)&lt;&lt;căn lề &lt;&lt;dữ liệu xuất;<br />Ghi chú:<br /><code>n&gt;=0 là đô rộng danh cho dữ liệu xuất gí trị nguyên.</code><br /><code>căn lề: Trái hoặc phải có giá trị&nbsp;</code>left&nbsp;hoặc&nbsp;right<br /><a href="https://dataonline.io.vn/#MLCD1_123">1.2.3. Số chữ số thập phân.</a><br />cout &lt;&lt;setprecision(n)&lt;&lt; fixed&lt;&lt;dữ liệu số thực;<br />n&gt;=0: là số chữ số phần thập phân được xuất ra.<br />fixed: là in số thập phân làm tròn đến n.</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;iomanip&gt;using namespace std;int main(){    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt; &quot;9+4 = &quot;&lt;&lt;9+4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;right&lt;&lt; &quot;9-4 = &quot;&lt;&lt;9-4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt; &quot;9*4 = &quot;&lt;&lt;9*4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;right&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9/4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;left&lt;&lt;setprecision(5)&lt;&lt; fixed&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl;    cout &lt;&lt;setprecision(1)&lt;&lt; fixed&lt;&lt; &quot;9/4 = &quot;&lt;&lt;9.0/4 &lt;&lt; endl;    cout &lt;&lt;setw(10)&lt;&lt;right&lt;&lt; &quot;9%4 = &quot;&lt;&lt;9%4 &lt;&lt; endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/pc4Iuv5.png" alt="" /><br />Kết quả:<br /><img src="https://i.imgur.com/x1XMhQF.jpg" alt="" /><br />Giải thích:&nbsp;#include &lt;iostream&gt;&nbsp;để dùng&nbsp;hàm xuất nhập cout và cin<br />using namespace std;&nbsp;Nếu không khai báo thì phải viết các lệnh như:&nbsp;std::cout &lt;&lt; &quot;9+4 = &quot;&lt;&lt;9+4 &lt;&lt; std::endl;<br />#include &lt;iomanip&gt;&nbsp;setw(n), ::setfill(ch), left, setprecision(n)<br /><a href="https://dataonline.io.vn/#CD1.2.4_ML">1.2.4. Chương trình con-Hàm.</a><br />- Chương trình con-Hàm là một nhóm lệnh, yêu cầu chương trình phải hoàn thành một công việc nào đó gồm hai loại:<br />+ Hàm có kiểu trả lại (trả blại dữ liệu thuộc kiểu dữ liệu của hàm)<br />+ Hàm không có kiểu trả lại (còn gọi là hàm trả lại kiểu void)<br />- Định nghĩa hàm:<br />kiểu_dữ_liệu_trả_lại tên_của_hàm(các_tham_số_của_hàm)<br />{<br />&nbsp; &nbsp; nội_dung_của_hàm;<br />&nbsp;……..<br />&nbsp; &nbsp;return&nbsp;kiểu_dữ_liệu_trả_lại; //nếu&nbsp;Hàm không có kiểu trả lại thì không có lệnh này<br />}<br />- Ví dụ&nbsp;Hàm có kiểu trả lại. Ví dụ tính tổng hai nguyên int số a và b. Kiểu trả lại là int</p><pre><code class="language-cpp">int Tong(int a, int b){    int S = 0;    S = a+b;    return S;}</code></pre><p><img src="https://i.imgur.com/t0iOwdg.png" alt="" /><br />- Ví dụ&nbsp;Hàm không có kiểu trả lại. Ví dụ xuất tổng hai nguyên số a và b ra màn hình.&nbsp;Kiểu trả lại là void</p><pre><code class="language-cpp">void Xuat(int a, int b){    int T = Tong(a,b);    cout&lt;&lt;&quot;Tong cua hai so nguyen &quot;&lt;&lt;a&lt;&lt;&quot; va &quot;&lt;&lt;b&lt;&lt;&quot; la: &quot;&lt;&lt;T;}</code></pre><p><img src="https://i.imgur.com/VXG9ekf.png" alt="" /><br />- Chương trình hoàn chỉnh</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int Tong(int a, int b){    int S = 0;    S = a+b;}void Xuat(int a, int b){    int T = Tong(a,b);    cout&lt;&lt;&quot;Tong cua hai so nguyen &quot;&lt;&lt;a&lt;&lt;&quot; va &quot;&lt;&lt;b&lt;&lt;&quot; la: &quot;&lt;&lt;T;}int main(){    Xuat(8, 9);    return 0;}</code></pre><p><img src="https://i.imgur.com/TDHBCRS.png" alt="" /><br />Kết quả<br /><img src="https://i.imgur.com/LuF9s9q.png" alt="" /><br /><a href="https://dataonline.io.vn/#MLCD2">2. Chủ đề 2. Kiểu dữ liệu và&nbsp;hằng, biến.</a><br /><a href="https://dataonline.io.vn/#MLCD21">2.1. Một số kiểu dữ liệu cơ bản thường dùng trong C++</a></p><figure class="table"><table><tbody><tr><td>TT</td><td>Kiểu dữ liệu</td><td>Mô tả</td><td>Ghi chú</td></tr><tr><td colspan="1" rowspan="2">1</td><td>int,&nbsp;long</td><td>-2147483648 tới 2147483647</td><td>Số nguyên</td></tr><tr><td>unsigned int,&nbsp;long</td><td>0 tới 4,294,967,295</td><td>Số nguyên</td></tr><tr><td>2</td><td>float</td><td>&nbsp;</td><td>Số thực</td></tr><tr><td colspan="1" rowspan="2">3</td><td>char</td><td>-127 tới 127</td><td>Kí tự</td></tr><tr><td>unsigned char</td><td>0 tới 255</td><td>Kí tự</td></tr><tr><td>4</td><td>string</td><td>255 kí tự, chỉ số từ 0</td><td>Xâu kí tự,&nbsp;#include &lt;string&gt;</td></tr><tr><td>5</td><td>bool</td><td>true hoặc false</td><td>Logic</td></tr><tr><td colspan="1" rowspan="2">6</td><td><span style="color:rgb(192,57,43);">long&nbsp;long</span></td><td><span style="color:rgb(192,57,43);">-9,223,372,036,854,775,808 đến 9,223,372,036,854,775,807</span></td><td>Số nguyên</td></tr><tr><td><span style="color:rgb(192,57,43);">unsigned long&nbsp;long</span></td><td><span style="color:rgb(192,57,43);">0 đến 18,446,744,073,709,551,615</span></td><td>Số nguyên</td></tr><tr><td colspan="1" rowspan="2">7</td><td>short</td><td>-32768 tới 32767</td><td>Số nguyên</td></tr><tr><td>unsigned&nbsp;short</td><td>0 tới 65,535</td><td>Số nguyên</td></tr><tr><td colspan="1" rowspan="2">8</td><td>double</td><td>&nbsp;</td><td>&nbsp;Số thực</td></tr><tr><td>long double</td><td>&nbsp;</td><td>Số thực</td></tr><tr><td>9</td><td>void</td><td>&nbsp;</td><td>&nbsp;&nbsp;Kiểu không có giá trị</td></tr></tbody></table></figure><p>Giải thích phạm vi của kiểu dữ liệu<br />1 bit trong lập trình sẽ biểu diễn được 2 giá trị: 0 hoặc 1. Tức là biểu diễn được 2^1 giá trị.<br />Vậy 2 bits sẽ biểu diễn được 4 giá trị: 00, 01, 10 và 11. Tức là biểu diễn được 2^2 giá trị.<br />Như vậy, 1 byte = 8 bits sẽ biểu diễn được 2^8 giá trị khác nhau.</p><p><strong>Số lượng giá trị mà n bit có thể biểu diễn được là 2^n</strong><br />Mà máy tính lưu giá trị mã bit 0 và 1. Tức dạng nhị phân. Do đó 00 = 0, 01 = 1, 10 = 2, 11 = 3 trong cơ số 10. Như vậy, giá trị lớn nhất mà 1 byte có thể biểu diễn là 11111111 = 2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 = 255.<br />Đó là lý do kiểu unsigned char có phạm vi giá trị từ 0 đến 255. Còn kiểu char do có cả miền âm nên sẽ bị chia đôi(-128 đến 127). Phần dương bị hụt 1 giá trị do cần biểu diễn cả số 0.<br /><a href="https://dataonline.io.vn/#MLCD1_122">2.2. Cách khai báo và sử dụng biến hằng, phép gán.</a><br />* Phép gán là dấu =<br />* Hằng:&nbsp;Hằng (constant) là đại lượng có giá trị không thay đổi trong suốt quá trình thực hiện chương trình.&nbsp;Từ khóa khai báo hằng: const.<br />- Cú pháp khai báo hằng:<br />const kiểu dữ liệu tên_hằng = giá trị;<br />Ví dụ:&nbsp;const double PI = 3.14159;<br />const int diem_thuong = 5;<br />hoặc<br />kiểu dữ liệu const tên_hằng = giá trị;<br />Ví dụ:&nbsp;double const&nbsp;&nbsp;PI = 3.14159;<br />int const diem_thuong = 5;<br />* Biến:&nbsp;Biến (variable) là đại lượng có giá trị có thể thay đổi trong khi thực hiện chương trình.<br />- Cú pháp khai báo biến:<br />kiểu dữ liệu tên_biến = giá trị;<br />kiểu dữ liệu tên_biến;<br />hoặc<br />kiểu dữ liệu danh_sách_tên_biến = giá trị;<br />kiểu dữ liệu danh_sách_tên_biến;<br />- Lưu ý: Danh sách tên biến cách nhau bởi dấu phẩy (,); giá trị có thể gán giá trị ban đầu hoặc không gán.<br />- Ví dụ:<br />int a = 0, b = 20; // Khai báo 2 biến kiểu số nguyên<br />float = 20.8;<br />char c = &#039;A&#039;;<br />string s = &quot;Thong bao&quot;;<br />hoặc&nbsp;<br />int a, b; // Khai báo 2 biến kiểu số nguyên<br />float c;<br />char c;<br />string s;<br /><a href="https://dataonline.io.vn/#MLCD23">2.3. Nhập dữ liệu từ bàn phím, từ tệp (đọc tệp) , xuất tệp (ghi tệp).</a><br />* Nhập dữ liệu từ bàn phím, lệnh: cin&gt;&gt;<br />Ví dụ: cin&gt;&gt;a;</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;iomanip&gt;using namespace std;const float pi = 3.14;int main(){    float ban_kinh = 0, dien_tich,chu_vi=0;;    string tb_dien_tich = &quot;Dien tich hinh tron la: &quot;;    string tb_chu_vi = &quot;Chu vi hinh tron la: &quot;;    cout &lt;&lt; &quot;Nhap ban kinh = &quot;;    cin&gt;&gt;ban_kinh;    dien_tich = pi*ban_kinh*ban_kinh;    chu_vi = 2*pi*ban_kinh;    cout &lt;&lt; tb_dien_tich&lt;&lt;dien_tich&lt;&lt;endl;    cout &lt;&lt; tb_chu_vi&lt;&lt;chu_vi&lt;&lt;endl;    cout &lt;&lt;setw(30)&lt;&lt;right&lt;&lt;setprecision(5)&lt;&lt;fixed&lt;&lt; tb_dien_tich&lt;&lt;dien_tich&lt;&lt;endl;    cout &lt;&lt;setprecision(5)&lt;&lt;fixed&lt;&lt; tb_chu_vi&lt;&lt;chu_vi&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/O2T9oI4.png" alt="" /><br /><br />&nbsp; <img src="https://i.imgur.com/wvhTS7q.png" alt="" /><br />* Nhập dữ liệu từ tệp (đọc tệp), xuất dữ liệu ra tệp (xuất tệp, ghi tệp)<br />- Sử dụng thư viện: #include &lt;fstream&gt;</p><figure class="table"><table><tr><th>Kiểu dữ liệu</th><th>Miêu tả</th></tr><tbody><tr><td>ofstream</td><td>Kiểu dữ liệu này biểu diễn Output File Stream và được sử dụng để tạo các file và để ghi thông tin tới các file đó</td></tr><tr><td>ifstream</td><td>Kiểu dữ liệu này biểu diễn Input File Stream và được sử dụng để đọc thông tin từ các file</td></tr><tr><td>fstream</td><td>Kiểu dữ liệu này nói chung biểu diễn File Stream, và có các khả năng của cả ofstream và ifstream, nghĩa là nó có thể tạo file, ghi thông tin tới file và đọc thông tin từ file</td></tr></tbody></table></figure><figure class="table"><table><tr><th>&nbsp;&nbsp;Chế độ</th><th>Miêu tả</th></tr><tbody><tr><td>ios::app&nbsp;&nbsp;</td><td>Chế độ Append. Tất cả output tới file đó được phụ thêm vào cuối file đó</td></tr><tr><td>ios::ate</td><td>Mở một file cho output và di chuyển điều khiển read/write tới cuối của file</td></tr><tr><td>ios::in</td><td>Mở một file để đọc</td></tr><tr><td>ios::out</td><td>Mở một file để ghi (tạo file mới nếu chưa tồn tại)</td></tr><tr><td>ios::trunc</td><td>Nếu file này đã tồn tại, nội dung của nó sẽ được cắt (truncate) trước khi mở file</td></tr></tbody></table></figure><p>Bước 1: Mở tập tin lên theo chức năng mong muốn (ghi hay đọc)<br />Bước 2: Thực hiện các lệnh ghi hoặc đọc<br />Bước 3: Đóng tập tin<br />1. Cách đọc tệp C++<br />B1. Mở tệp đọc<br />ifstream inFile(“Data.txt”,ios::in); &nbsp;// Mở tập tin có tên Data.txt, tập tin này nằm trong cùng thư mục tập tin code .cpp<br />//hoặc<br />ifstream inFile(“D:Data.txt”,ios::in);//Mở tập tin có tên Data.txt trong ổ đĩa D<br />hoặc<br />ifstream &nbsp;inFile;<br />inFile.open(“Data.txt”,ios::in);<br />B2.&nbsp;Việc đọc nội dung tập tin được chia làm 2 cách, bạn có thể đọc từng ký tự, từng byte trong tập tin cho đến hết tập tin. Hoặc bạn có thể đọc từng hàng của tập tin cho đến hết tập tin.<br />+ Sử dụng phương thức &gt;&gt;.<br />Việc đọc nội dung tập tin được chia làm 2 cách, bạn có thể đọc từng ký tự, từng byte trong tập tin cho đến hết tập tin. Hoặc bạn có thể đọc từng hàng của tập tin cho đến hết tập tin.<br />int songuyen;<br />inFile&gt;&gt;songuyen;<br />+ Sử dụng phương thức get(char KT)<br />ifstream inFile(“Data.txt”,ios::in);<br />char kt;<br />inFile.get(kt);// Đọc ký tự từ tập tin và lưu vào biến kt<br />cout&lt;&lt;kt;// In dữ liệu đọc được lên màn hình<br />+ Sử dụng phương thức getline(char CH&#91;&#93;, int N)<br />ifstream inFile(“Data.txt”,ios::in);<br />char CHUOI&#91;100&#93;;<br />inFile.getline(CHUOI,100);//Đọc dữ liệu từ tập tin theo dòng vào chuỗi với độ dài chuỗi là 100 ký tự<br />cout&lt;&lt;CHUOI&lt;&lt;endl; //In dữ liệu đọc được lên màn hình<br />B3: Đóng tập tin<br />inFile.close();<br />2.&nbsp;Cách ghi tập tin trong C++<br />B1. Mở tệp<br />ofstream outFile(“Data.txt”, ios::out);//Mở tập tin để ghi nội dung vào<br />B2. Ghi nội dung<br />+ Sử dụng phương thức operator &lt;&lt;<br />ofstream outFile(“Data.txt”,ios::out);<br />char KT&#91;&#93;=“Trung tam tin hoc Tay Viet”;<br />outFile &lt;&lt; KT;//Ghi toàn bộ nội dung trong chuỗi KT vào tập tin<br />+ Sử dụng phương thức put(char KT)<br />ofstream outFile(“Data.txt”,ios::out);<br />char kt;<br />get(kt); //Nhập ký tự cần ghi vào tập tin<br />outFile.put(kt);<br />B3: Đóng tập tin<br />outFile.close();<br />Hàm eof()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cấu trúc hàm: &nbsp;bool eof() const;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hàm eof() là hàm kiểm tra đã hết tập tin chưa. Nếu đã hết tập tin thì hàm trả về true ( tương đương với giá trị khác 0), ngược lại nếu chưa hết tập tin thì kết quả sẽ trả về false ( tương đương với giá trị là 0).<br />Hàm fail()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cấu trúc hàm: &nbsp;bool fail() const;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hàm fail() là hàm phát hiện có xảy ra lỗi không trầm trọng trong quá trình đọc tập tin. Nếu phát hiện ra lỗi trong quá trình đọc tập tin thì hàm trả về true ( tương đương với giá trị khác 0), ngược lại nếu không thì kết quả sẽ trả về false ( tương đương với giá trị là 0). Ví dụ trong chương trình, chúng ta đọc tập tin sau khi đã đóng thì sẽ xuất hiện lỗi và hàm này sẽ cho giá trị true<br />Hàm bad()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cấu trúc hàm: &nbsp;bool bad() &nbsp;const;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hàm bad() là hàm phát hiện có xảy ra lỗi trầm trọng trong quá trình ghi tập tin. Nếu phát hiện ra lỗi trong quá trình ghi tập tin thì hàm trả về true ( tương đương với giá trị khác 0), ngược lại nếu không thì kết quả sẽ trả về false ( tương đương với giá trị là 0). Ví dụ trong chương trình, chúng ta ghi nội dung vào tập tin đã đóng thì sẽ xuất hiện lỗi và hàm này sẽ cho giá trị true.<br />Hàm good()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cấu trúc hàm: &nbsp;bool good() const;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hàm good() là hàm kiểm tra hoàn thiện việc ghi và đọc tập tin. Hàm trả về true ( tương đương với giá trị khác 0) khi tất cả các hàm eof(), fail() và bad() đều trả về giá trị 0, ngược lại chỉ cần 1 trong các hàm eof(), fail() và bad() trả về 1 thì kết quả của hàm good sẽ trả về false ( tương đương với giá trị là 0).<br />Con trỏ trong tập tin<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Trong quá trình đọc và ghi File trong lập trình C++ thì chúng ta cần phải biết con trỏ tập tin hiện đang ở vị trí nào, điều này sẽ giúp ta rất nhiều trong việc tương tác với tập tin. Chẳng hạn bạn muốn thêm, thay đổi hoặc xóa một nội dung nào đó thì chúng ta cần phải biết chính xác vị trí cần thao tác để tránh việc làm sai dữ liệu.<br />a. Hàm trong class ifstream<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.1 Hàm tellg()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hàm này được sử dụng để tìm vị trí hiện tại của con trỏ tập tin. Vị trí hiện tại của con trỏ trong tập tin chính là tổng số byte tính từ đầu tập tin đến vị trị của con trỏ.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.2 Hàm seekg()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hàm seekg() là hàm được sử dụng để dịch chuyên con trỏ đến vị trí nào đó. Việc di chuyển con trỏ trong tập tin cần phải có vị trí làm mốc, vị trí này có thể là đầu tập tin, cuối tập tin hoặc là vị trí hiện tại của con trỏ, điều này phụ thuộc vào tham số trong hàm. Hàm seekg có 2 cú pháp như sau:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cú pháp 1: &nbsp;seekg(&lt;số byte cần dịch chuyển&gt;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Với cú pháp 1 này thực hiện chức năng dịch chuyển con trỏ sang phải với số byte đúng bằng giá trị truyền vào. Mốc dịch chuyển là vị trí đầu của tập tin.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cú pháp 2: seekg(&lt;số byte cần dịch chuyển&gt;,&lt;mốc dịch chuyển&gt;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mốc dịch chuyển là 1 trong các tham số sau:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ ios::beg : Mốc là vị trí đầu tập tin<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ ios::cur : Mốc là vị trí hiện tại của con trỏ<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ ios::end : Mốc là vị trí cuối của tập tin<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b. Hàm trong class ofstream<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.1 Hàm tellp()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tương tự như Hàm này được sử dụng để tìm vị trí hiện tại của con trỏ tập tin. Vị trí hiện tại của con trỏ trong tập tin chính là tổng số byte tính từ đầu tập tin đến vị trị của con trỏ.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.2 Hàm seekp()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cũng giống như hàm seekg(), hàm seekp() cũng được sử dụng để dịch chuyên con trỏ đến vị trí nào đó trong tập tin. Và trong trường hợp này cũng sử dụng 2 cú pháp như trên<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cú pháp 1: &nbsp;seekp(&lt;số byte cần dịch chuyển&gt;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Với cú pháp 1 này thực hiện chức năng dịch chuyển con trỏ sang phải với số byte đúng bằng giá trị truyền vào. Mốc dịch chuyển là vị trí đầu của tập tin.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cú pháp 2: seekp(&lt;số byte cần dịch chuyển&gt;,&lt;mốc dịch chuyển&gt;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mốc dịch chuyển là 1 trong các tham số sau:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ ios::beg : Mốc là vị trí đầu tập tin<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ ios::cur : Mốc là vị trí hiện tại của con trỏ<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ ios::end : Mốc là vị trí cuối của tập tin<br />Ví dụ hoàn chỉnh:</p><pre><code class="language-cpp">#include&lt;iostream&gt;#include&lt;fstream&gt;#include&lt;cstring&gt;#include&lt;stdio.h&gt;#include&lt;conio.h&gt;using namespace std;int main(){    ofstream outFile(&quot;Data.txt&quot;,ios::out);    if(!outFile)        cout&lt;&lt;&quot;Khong mo duoc tap tin&quot;;    else    {        char CH&#91;100&#93;;        int tuoi;        cout&lt;&lt;&quot;Nhap vao ten cua ban: &quot;;        gets(CH);        outFile&lt;&lt;CH&lt;&lt;endl;        cout&lt;&lt;&quot;Nhap vao tuoi ban: &quot;;        cin&gt;&gt;tuoi;        outFile&lt;&lt;tuoi;        cout&lt;&lt;&quot;Da ghi tap tin thanh cong&quot;&lt;&lt;endl;        outFile.close();    }    ifstream inFile(&quot;Data.txt&quot;,ios::in);    if(!inFile)        cout&lt;&lt;&quot;Khong mo duoc tap tin&quot;&lt;&lt;endl;    else    {        cout&lt;&lt;&quot;Noi dung doc duoc la:&quot;;        char Ten&#91;100&#93;,age;        inFile.getline(Ten,100);        inFile.get(age);        cout&lt;&lt;&quot;Ten cua ban: &quot;&lt;&lt;Ten&lt;&lt;endl;        cout&lt;&lt;&quot;Tuoi ban: &quot;&lt;&lt;age;        inFile.close();    }}</code></pre><p><img src="https://i.imgur.com/emVkHfq.png" alt="" /><br /><a href="https://dataonline.io.vn/#MLCD24">2.4. Củng cố và luyện tập lại các phép toán số học.</a><br />Câu 1. Cho số nguyên N trong tệp Cau1.INP. Xuất ra màn hình kết quả của N*N</p><figure class="table"><table><tbody><tr><td>INPUT</td><td>OUTPUT</td></tr><tr><td>6</td><td>36</td></tr><tr><td>8</td><td>64</td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream  fin;    fin.open(&quot;BAI1.INP&quot;,ios::in);    int N;    if(fin.fail())    {        cout&lt;&lt;&quot;Loi mo tep&quot;;    }    else    {        fin&gt;&gt;N;        cout&lt;&lt;&quot;N*N = &quot;&lt;&lt;N*N;    }    fin.close();    return 0;}</code></pre><p><img src="https://i.imgur.com/NEPLmLx.png" alt="" /><br />Câu 2. Cho hai số nguyên a và b trong tệp Cau2.INP, mỗi số một dòng. Xuất ra màn hình kết quả tích a và b ra màn hình</p><figure class="table"><table><tbody><tr><td>INPUT</td><td>OUTPUT</td></tr><tr><td>6<br />9</td><td>54</td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    int a,b;    ifstream docvao;    docvao.open(&quot;Cau2.INP&quot;,ios::in);    if(docvao.fail())    {        cout&lt;&lt;&quot; loi tep &quot;;    }    else    {        docvao&gt;&gt;a;        docvao&gt;&gt;b;        int tich= a*b;        cout&lt;&lt;tich;    }    docvao.close();    return 0;}</code></pre><p><img src="https://i.imgur.com/fiAVKgt.png" alt="" /><br />Câu 3. Cho hai số nguyên a và b trong tệp Cau3.INP, mỗi số một dòng. Ghi kết quả tính tổng và tích a và b ra tệp Cau3.OUT, mỗi kết quả ghi một dòng.</p><figure class="table"><table><tbody><tr><td>Cau3.INP</td><td>Cau3.OUT</td></tr><tr><td>6<br />9</td><td>15<br />54</td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream fin;    ofstream fout;    fin.open(&quot;Cau3.INP&quot;,ios::in);    fout.open(&quot;Cau3.OUT&quot;,ios::out);    if(fin.fail())    {        fout&lt;&lt;&quot;Khong mo duoc tep&quot;;    }    else    {        int a, b, Tong = 0, Tich = 0;        fin&gt;&gt;a;        fin&gt;&gt;b;        Tong=a+b;        Tich=a*b;        fout&lt;&lt;Tong&lt;&lt;endl;        fout&lt;&lt;Tich;    }    fin.close();    fout.close();    return 0;}</code></pre><p><img src="https://i.imgur.com/DNODqWB.png" alt="" /><br /><a href="https://dataonline.io.vn/#MLCD3">3. Chủ đề 3. Phép so sánh, phép toán&nbsp;logic và câu&nbsp;lệnh điều kiện, lệnh lặp.</a><br /><a href="https://dataonline.io.vn/#MLCD31">3.1. Các phép so sánh và phép toán logic&nbsp;trong C++</a><br />&nbsp;- Các phép so sánh (toán tử quan hệ), phép logic (toán tử logic).<br />- Câu lệnh điều kiện, lặp.<br />Các phép so sánh (toán tử quan hệ)<br />Ví dụ: int a = 10, int b= 20;</p><figure class="table"><table><tbody><tr><td>Phép so sánh</td><td>Kí hiệu<br />(toán tử so sánh)</td><td>Ví dụ&nbsp;</td><td>Kết qủa</td></tr><tr><td>Bằng</td><td>==</td><td>a==b</td><td>false</td></tr><tr><td>Khác</td><td>!=</td><td>a!=</td><td>true</td></tr><tr><td>Lớn hơn</td><td>&gt;</td><td>a&gt;b</td><td>false</td></tr><tr><td>Nhỏ hơn</td><td>&lt;</td><td>a&lt;b</td><td>true</td></tr><tr><td>Lớn hơn hoặc bằng</td><td>&gt;=</td><td>a&gt;=</td><td>false</td></tr><tr><td>Nhỏ&nbsp;hơn hoặc bằng</td><td>&lt;=</td><td>a&lt;=b</td><td>true</td></tr></tbody></table></figure><p>Phép logic (toán tử logic)<br />Ví dụ: A = true; B = false</p><figure class="table"><table><tbody><tr><td>Phép Logic</td><td>Kí kiệu<br />(toán tử logic)</td><td>Ví dụ</td><td>Kết quả</td></tr><tr><td>AND (Và)</td><td>&amp;&amp;</td><td>A&amp;&amp;B</td><td>false</td></tr><tr><td>OR (Hoặc)</td><td>||</td><td>A||B</td><td>true</td></tr><tr><td colspan="1" rowspan="2">NOT (Phủ định)</td><td colspan="1" rowspan="2">!</td><td>!A</td><td>false</td></tr><tr><td>!B</td><td>true</td></tr></tbody></table></figure><p>Ví dụ:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    int b=0;    cout&lt;&lt;&quot;Nhap a = &quot;;    cin&gt;&gt;a;    cout&lt;&lt;&quot;Nhap b = &quot;;    cin&gt;&gt;b;    if((a&gt;0)&amp;&amp;(b&gt;0))    {        cout&lt;&lt;&quot;a va b deu lon hon 0&quot;&lt;&lt;endl;    }    if((a&gt;0)||(b&gt;0))    {        cout&lt;&lt;&quot;a hoac b lon hon 0&quot;&lt;&lt;endl;    }    if(!(a&gt;0))    {        cout&lt;&lt;&quot;Xuat ra man hinh khi a nho hon 0&quot;&lt;&lt;endl;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/9ksXZYX.png" alt="" /><br /><a href="https://dataonline.io.vn/#MLCD32">3.2. Câu lệnh điều kiện if, switch case ...</a><br />a) Câu lệnh điều kiện if<br />Dạng thiếu:<br />if (điều kiện)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;nội dung khi đk đúng;<br />}<br />Ví dụ:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    if(a&gt;b)    {        cout&lt;&lt;&quot;So a lon hon so b&quot;;    }    return 0;}</code></pre><p><strong><img src="https://i.imgur.com/cSxzz1p.png" alt="" /></strong><br />Dạng đủ:<br />if (điều kiện)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;nội dung khi đk đúng;<br />}<br />else<br />{<br />&nbsp;&nbsp;&nbsp;nội dung khi đk sai;<br />}<br />Ví dụ:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    if(a&gt;b)    {        cout&lt;&lt;&quot;So a lon hon so b&quot;;    }    else    {        cout&lt;&lt;&quot;So a nho hon so b&quot;;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/4eD49Pb.png" alt="" /><br />b) Câu lệnh điều kiện switch ... case<br />switch (biến hoặc biểu thức kiểm tra)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case giá trị 1:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lệnh thứ 1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case giá trị 2:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lệnh thứ 2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case giá trị 3:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lệnh thứ 3;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case giá trị thứ n:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lệnh thứ n;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lệnh thực hiện khi&nbsp;&nbsp;các lựa chọn trên đều sai; //phần default là không bắt buộc<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />Ví dụ:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    char dau;    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    cout&lt;&lt;&quot;Nhap dau phep toan la: &quot;;    cin&gt;&gt;dau;    switch (dau)    {    case &#039;+&#039;:        S= a + b;        break;    case &#039;-&#039;:        S=  a - b;        break;    case &#039;*&#039;:        S=  a * b;        break;    case &#039;/&#039;:        if (b == 0)        {            cout &lt;&lt; &quot;Phep chia cho 0!&quot;;        }        if (b != 0)        {            S=  a / b;        }    default:        cout &lt;&lt; &quot;Phep tinh khong hop le!&quot;;    }    cout&lt;&lt;&quot;Ket qua phep tinh la: &quot;&lt;&lt;S;    return 0;}</code></pre><p><img src="https://i.imgur.com/Jg5RQVn.png" alt="" /><br />c) Câu lệnh điều kiện thay thế cặp if ..else<br />c = (a&gt;b? giá trị khi đúng : giá trị khi sai);<br />Ví dụ:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    S=(a&gt;b?a-b:a+b);    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/xZwZgOF.png" alt="" /><br />thay thê cặp lệnh if ...else sau:</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0, b=0, S=0;    cout &lt;&lt; &quot;a = &quot;;    cin&gt;&gt;a;    cout &lt;&lt; &quot;b = &quot;;    cin&gt;&gt;b;    if(a&gt;b)    {        S=a-b;    }    else    {        S=a+b;    }    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><strong>&nbsp;<img src="https://i.imgur.com/V3BXkyC.png" alt="" /></strong><br />Câu 1. Cho số nguyên N trong tệp Cau1.INP. Xuất ra màn hình kết quả kiểm tra xem N là số chẵn hay số ra màn hình</p><figure class="table"><table><tbody><tr><td>INPUT</td><td>OUTPUT</td></tr><tr><td>6</td><td>6 la so chan</td></tr><tr><td>7</td><td>7 la s</td></tr></tbody></table></figure><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream  fin;    fin.open(&quot;BAI1.INP&quot;,ios::in);    int N;    if(fin.fail())    {        cout&lt;&lt;&quot;Loi mo tep&quot;;        fin.close();    }    else    {        fin&gt;&gt;N;        fin.close();    }    if(N%2==0)    {        cout&lt;&lt;N&lt;&lt;&quot; la so chan&quot;;    }    else    {        cout&lt;&lt;N&lt;&lt;&quot; la so le&quot;;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/cRPaIY7.png" alt="" /><br /><a href="https://dataonline.io.vn/#MLCD33">3.3. Câu lệnh lặp for, while, do - while.</a><br />a) Câu lệnh for<br />for (bien=giá trị; dieu_kien; tang_giam)&nbsp;<br />&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;cac_lenh;&nbsp;<br />&nbsp;}<br />Ví dụ xuất ra màn hình các số từ 1 đến 15</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    for(a = 1; a &lt;= 15; a++)    {        cout &lt;&lt; &quot;Gia tri cua a la: &quot; &lt;&lt; a &lt;&lt; endl;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/Y9gr30D.png" alt="" /><br />b) Câu lệnh lặp while<br />while(dieu_kien)&nbsp;<br />&nbsp;{&nbsp;<br />&nbsp;&nbsp;&nbsp;cac_lenh;&nbsp;<br />&nbsp;}<br />Ví dụ xuất ra màn hình các số từ 1 đến 15</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    while(a &lt; 15)    {        a++;        cout &lt;&lt; &quot;Gia tri cua a la: &quot; &lt;&lt; a &lt;&lt; endl;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/8TstUyW.png" alt="" /><br />c) Câu lệnh lặp do while<br />- Câu lệnh này được thực hiện ít nhất 01 lần<br />do<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cac_lenh;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;while(dieu_kien);<br />Ví dụ xuất ra màn hình các số từ 1 đến 15</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int a=0;    do    {        a++;        cout &lt;&lt; &quot;Gia tri cua a la: &quot; &lt;&lt; a &lt;&lt; endl;    }    while(a&lt;15);}</code></pre><p><img src="https://i.imgur.com/t46p98Y.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_CD4CT">4. Chủ đề 4. Mảng một chiều (số nguyên, số thực).</a><br />Mảng là:<br />Một kiểu dữ liệu có cấu trúc do người lập trình định nghĩa.<br />Biểu diễn một dãy các biến có cùng kiểu. Ví dụ: dãy các số nguyên, dãy số thực, dãy các ký tự…<br />Kích thước được xác định ngay khi khai báo và không bao giờ thay đổi (mảng tĩnh).<br />Mục tiêu:<br />- Cách khai báo và sử dụng mảng, chèn thêm, xóa phần tử ở vị trí xác định, tìm kiếm trong mảng ...<br />Kiến thức:<br />- Cú pháp khai báo mảng:&nbsp;kiểu dữ liệu tên biến mảng&#91;số phần tử&#93;;<br />Một mảng liên tục có chỉ số từ 0 đến &lt;tổng số phần tử&gt; - 1.<br />Bộ nhớ sử dụng = &lt;tổng số phần tử&gt; * sizeof(&lt;kiểu cơ sở&gt;).<br /><a href="https://dataonline.io.vn/#ML_CD41">4.1. Khai báo, nhập, xuất mảng.</a><br />Cách 1: Khởi tạo giá trị cho mọi phần tử của mảng:</p><pre><code class="language-cpp">int array1&#91;4&#93;;int array2&#91;4&#93; = { 5, 8, 2, 7 };</code></pre><p>Cách 2: Khởi tạo giá trị cho một số phần tử đầu mảng:</p><pre><code class="language-cpp">int array1&#91;4&#93; = { 5, 8 };</code></pre><p>Cách 3: Khởi tạo giá trị 0 cho mọi phần tử của mảng</p><pre><code class="language-cpp">int array1&#91;4&#93; = { };</code></pre><p>Cách 4: Tự động xác định số lượng phần tử</p><pre><code class="language-cpp">int array1&#91;&#93; = { 5, 8, 2, 7 };</code></pre><p>Cách 5: Sử dụng khởi tạo đồng nhất trong C++11.</p><pre><code class="language-cpp">int array1&#91;4&#93; { 5, 8, 2, 7 }; // 5 8 2 7int array2&#91;4&#93; { 5, 8 }; // 5 8 0 0int array3&#91;4&#93; { }; // 0 0 0 0int array4&#91;&#93; { 5, 8, 2, 7 }; // 5 8 2 7</code></pre><p>Nhập (gán) dữ liệu cho mảng 1 chiều<br />tên biến mảng&#91;chỉ số&#93; = giá trị;<br />Truy cập phần tử mảng 1 chiều<br />tên biến mảng&#91;chỉ số&#93;;<br />Ví dụ về khai báo, nhập, xuất giá trị cho phần tử có trong mảng.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;const int sopt=200;int main(){    int a&#91;sopt&#93;;    int n=0, i=0;    n = 5;    cout&lt;&lt;&quot;a&#91;0&#93; = &quot;;    cin&gt;&gt;a&#91;0&#93;;    cout&lt;&lt;&quot;a&#91;1&#93; = &quot;;    cin&gt;&gt;a&#91;1&#93;;    cout&lt;&lt;&quot;a&#91;2&#93; = &quot;;    cin&gt;&gt;a&#91;2&#93;;    cout&lt;&lt;&quot;a&#91;3&#93; = &quot;;    cin&gt;&gt;a&#91;3&#93;;    cout&lt;&lt;&quot;a&#91;4&#93; = &quot;;    cin&gt;&gt;a&#91;4&#93;;    cout&lt;&lt;a&#91;0&#93;&lt;&lt;endl;    cout&lt;&lt;a&#91;3&#93;&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/tHiOI23.png" alt="" /><br />Ví dụ về khai báo, nhập, xuất giá trị các phần tử có trong mảng dùng vòng lặp.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;const int sopt=200;int main(){    int a&#91;sopt&#93;;    int n=0, i=0;    cout&lt;&lt;&quot;Nhap so phan tu cua mang, n = &quot;;    cin&gt;&gt;n;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;endl;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/X8t7s10.png" alt="" /></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    Xuat(a,n);    return 0;}</code></pre><p><img src="https://i.imgur.com/nwG2ugO.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_CD42">4.2. Sắp xếp tăng giảm (tự code sắp xếp, sử dụng hàm có sẵn - ưu tiên sử dụng hàm sort có sẵn).</a><br />* Sắp xếp mảng tự code (chưa được tốt, dễ hiểu dành cho mọi đối tượng học sinh mới làm quen)</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void Sapxeptang(int a&#91;&#93;, int n){    int i = 0,j = 0, temp = 0;    for(i=0; i&lt;n-1; i++)    {        for(j=i+1; j&lt;n; j++)        {            if(a&#91;i&#93;&gt;a&#91;j&#93;)            {                temp=a&#91;i&#93;;                a&#91;i&#93;=a&#91;j&#93;;                a&#91;j&#93;=temp;            }        }    }}void Sapxepgiam(int a&#91;&#93;, int n){    int i = 0,j = 0, temp = 0;    for(i=0; i&lt;n-1; i++)    {        for(j=i+1; j&lt;n; j++)        {            if(a&#91;i&#93;&lt;a&#91;j&#93;)            {                temp=a&#91;i&#93;;                a&#91;i&#93;=a&#91;j&#93;;                a&#91;j&#93;=temp;            }        }    }}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    Sapxeptang(a,n);    Xuat(a,n);    cout &lt;&lt;endl;    Sapxepgiam(a,n);    Xuat(a,n);    return 0;}</code></pre><p><img src="https://i.imgur.com/XmwpMCx.png" alt="" /><br /><img src="https://i.imgur.com/Q2rO40N.png" alt="" /><br /><strong><img src="https://i.imgur.com/ZR9bF1y.png" alt="" /></strong><br />* Sắp xếp mảng tự code (điều chỉnh, bổ sung cách trên để chương trình chạy tốt hơn, sắp xếp nhanh hơn).</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void Sapxeptang(int a&#91;&#93;, int n){    int i = 0,j = 0, temp = 0, minindex=0;    for(i=0; i&lt;n-1; i++)    {        minindex = i;        for(j=i+1; j&lt;n; j++)        {            if(a&#91;minindex&#93;&gt;a&#91;j&#93;)            {                minindex=j;            }        }        if(minindex&gt;i)        {            temp=a&#91;minindex&#93;;            a&#91;minindex&#93;=a&#91;i&#93;;            a&#91;i&#93;=temp;        }    }}void Sapxepgiam(int a&#91;&#93;, int n){    int i = 0,j = 0, temp = 0, minindex= 0;    for(i=0; i&lt;n-1; i++)    {        minindex= i;        for(j=i+1; j&lt;n; j++)        {            if(a&#91;minindex&#93;&lt;a&#91;j&#93;)            {                minindex=j;            }        }        if(minindex&gt;i)        {            temp=a&#91;minindex&#93;;            a&#91;minindex&#93;=a&#91;i&#93;;            a&#91;i&#93;=temp;        }    }}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    Sapxeptang(a,n);    Xuat(a,n);    cout &lt;&lt;endl;    Sapxepgiam(a,n);    Xuat(a,n);    return 0;}</code></pre><p><img src="https://i.imgur.com/I5q3mKF.png" alt="" /><br /><br />Dùng hàm có sẵn sort phải nhập 02 thư viện sau:<br />#include&lt;algorithm&gt;<br />#include&lt;functional&gt; // std::greater</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include&lt;algorithm&gt;#include&lt;functional&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void SapxeptangSort(int a&#91;&#93;, int n){    sort(a, a+n);}void SapxepgiamSort(int a&#91;&#93;, int n){    sort(a, a+n, greater&lt;int&gt;());}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    SapxeptangSort(a,n);    Xuat(a,n);    cout &lt;&lt;endl;    SapxepgiamSort(a,n);    Xuat(a,n);    return 0;}</code></pre><p><img src="https://i.imgur.com/sgdDEXk.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_CD43">4.3.Kiểm tra xem giá trị X có xuất hiện trong mảng hay không? Xuất hiện bao nhiêu lần và các vị trị xuất hiện (nếu có)?</a></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Tim(int a&#91;&#93;, int n, int X){    int i =0, solan = 0, vitri&#91;n&#93;, vt =0;    for(i=0; i&lt;n; i++)    {        if(X==a&#91;i&#93;)        {            vitri&#91;vt&#93;=i;            vt++;            solan++;        }    }    if(solan&gt;0)    {        cout&lt;&lt;&quot;Gia tri &quot;&lt;&lt;X&lt;&lt;&quot; xuat hien trong mang &quot;&lt;&lt;solan&lt;&lt;&quot; lan. Tai cac vi tri: &quot;;        for(i=0; i&lt;solan; i++)        {            cout&lt;&lt;vitri&#91;i&#93;&lt;&lt;&quot; &quot;;        }    }    else    {        cout&lt;&lt;&quot;Gia tri &quot;&lt;&lt;X&lt;&lt;&quot; khong xuat hien trong mang&quot;;    }}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n, X;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    cout &lt;&lt; &quot;Nhap gia tri muon tim, X = &quot;;    cin&gt;&gt;X;    Tim(a,n,X);    cout &lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/uX9uO7G.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_CD44">4.4. Chèn thêm phần tử X vào mảng (tại vị trí nào đó hoặc mảng đã sắp xếp sao cho vẫn đảm bảo thứ tự sắp xếp).</a><br />*&nbsp; Chèn thêm phần tử X vào mảng (tại một vị trí chỉ định nào đó)&nbsp;</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void Chentaivitri(int a&#91;&#93;,int &amp;n, int vitri, int giatri){    int i;    for(i=n; i&gt;vitri; i--)    {        a&#91;i&#93;=a&#91;i-1&#93;;    }    a&#91;vitri&#93;=giatri;    n++;}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n, vitri, giatri;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    cout &lt;&lt; &quot;Nhap vi tri muon chen, vitri = &quot;;    cin&gt;&gt;vitri;    cout &lt;&lt; &quot;Nhap gia tri muon chen, giatri = &quot;;    cin&gt;&gt;giatri;    Chentaivitri(a,n,vitri, giatri);    Xuat(a,n);    return 0;}</code></pre><p><img src="https://i.imgur.com/H9dHgjY.png" alt="" /><br />* Chèn thêm phần tử X vào mảng (đã sắp xếp sao cho vẫn đảm bảo thứ tự sắp xếp).</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include&lt;algorithm&gt;#include&lt;functional&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void Chentaivitri(int a&#91;&#93;,int &amp;n, int vitri, int giatri){    int i;    for(i=n; i&gt;vitri; i--)    {        a&#91;i&#93;=a&#91;i-1&#93;;    }    a&#91;vitri&#93;=giatri;    n++;}void Sapxep(int a&#91;&#93;, int n, bool tangdan){    if(tangdan)    {        sort(a, a+n);    }    else    {        sort(a, a+n, greater&lt;int&gt;());    }    Xuat(a,n);    cout &lt;&lt;endl;}void Chendasapxep(int a&#91;&#93;, int &amp;n, int giatri, bool tangdan){    int i;    int vitri;    if(tangdan)    {        for(i=0; i&lt;n; i++)        {            if(a&#91;i&#93; &gt; giatri)            {                vitri =i;                break;            }        }    }    else    {        for(i=0; i&lt;n; i++)        {            if(a&#91;i&#93; &lt; giatri)            {                vitri =i;                break;            }        }    }    Chentaivitri(a,n,vitri,giatri);    Xuat(a,n);    cout &lt;&lt;endl;}int main(){    bool tangdan =false;    const int sophantu = 100;    int a&#91;sophantu&#93;, n, vitri, giatri;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    char tang;    cout &lt;&lt; &quot;Sap xep tang dan (C/K)?&quot;;    cin&gt;&gt;tang;    if((tang == &#039;C&#039;)||(tang == &#039;c&#039;))    {        tangdan =true;    }    Sapxep(a, n, tangdan);    cout &lt;&lt; &quot;Nhap gia tri, giatri = &quot;;    cin&gt;&gt;giatri;    Chendasapxep(a, n,giatri, tangdan);    return 0;}</code></pre><p><img src="https://i.imgur.com/jAQjl22.png" alt="" /><br /><img src="https://i.imgur.com/QKlgrFl.png" alt="" /><br /><a href="https://dataonline.io.vn/#CD4ML45">4.5. Xóa phần tử của mảng</a> (tại vị trí nào đó, xóa các phần tử trùng lặp - sau khi xóa, mỗi phần tử trong mảng là duy nhất).<br />* Xóa tại vị trí được chỉ định nào đó.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void Xoataivitri(int a&#91;&#93;, int &amp;n, int vitri){    int i =0;    for(i=vitri; i&lt;n; i++)    {        a&#91;i&#93;=a&#91;i+1&#93;;    }    n--;    Xuat(a,n);}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n, vitri;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    do    {        cout &lt;&lt; &quot;Nhap vi tri muon xoa, vitri = &quot;;        cin&gt;&gt;vitri;    }    while ((vitri&lt;0)||(vitri&gt;n-1));    Xoataivitri(a,n,vitri);    return 0;}</code></pre><p><img src="https://i.imgur.com/MZqvb88.png" alt="" /><br />* Xóa các phần tử trùng lặp - sau khi xóa, mỗi phần tử trong mảng là duy nhất.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}void Xuat(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;a&#91;i&#93;&lt;&lt;&quot; &quot;;    }}void Xoataivitri(int a&#91;&#93;, int &amp;n, int vitri){    int i =0;    for(i=vitri; i&lt;n; i++)    {        a&#91;i&#93;=a&#91;i+1&#93;;    }    n--;    Xuat(a,n);}void Xoatrunglap(int a&#91;&#93;, int &amp;n){    for(int i=0; i&lt;n; i++)    {        for(int j=i+1; j&lt;n;)        {            if(a&#91;i&#93;==a&#91;j&#93;)            {                for(int k=j; k&lt;n; k++)                {                    a&#91;k&#93;=a&#91;k+1&#93;;                }                n--;            }            else            {                j++;            }        }    }    Xuat(a,n);}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n, vitri;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    do    {        cout &lt;&lt; &quot;Nhap vi tri muon xoa, vitri = &quot;;        cin&gt;&gt;vitri;    }    while ((vitri&lt;0)||(vitri&gt;n-1));    Xoataivitri(a,n,vitri);    cout &lt;&lt;endl;    char OK;    cout &lt;&lt; &quot;Co xoa cac pah tu trung lap khong (C/K)?&quot;;    cin&gt;&gt;OK;    if((OK==&#039;c&#039;)||(OK==&#039;C&#039;))    {        Xoatrunglap(a,n);    }    return 0;}</code></pre><p><img src="https://i.imgur.com/OZLsHvO.png" alt="" /><br /><img src="https://i.imgur.com/NSm4I1c.png" alt="" /><br /><a href="https://dataonline.io.vn/#CD4ML46">4.6. Kiểm tra xem có phải là mảng đối xứng không?</a></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;void Nhap(int a&#91;&#93;, int n){    int i =0;    for(i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;a&#91;&quot;&lt;&lt;i&lt;&lt;&quot;&#93; = &quot;;        cin&gt;&gt;a&#91;i&#93;;    }}bool Checkdoixung(int a&#91;&#93;, int n){    bool ladx=true;    int den =n/2, i;    for(i=0; i&lt;den; i++)    {        if(a&#91;i&#93; != a&#91;n - i - 1&#93;)        {            ladx=false;            break;        }    }    return ladx;}int main(){    const int sophantu = 100;    int a&#91;sophantu&#93;, n, vitri, giatri;    cout &lt;&lt; &quot;Nhap so phan tu thuc co, n = &quot;;    cin&gt;&gt;n;    Nhap(a,n);    if(Checkdoixung(a, n))    {        cout &lt;&lt;&quot;La mang doi xung&quot;&lt;&lt;endl;    }    else    {        cout &lt;&lt;&quot;Khong phai mang doi xung&quot;&lt;&lt;endl;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/Rpdmk8m.png" alt="" /><br /><br /><a href="https://dataonline.io.vn/#CD4ML47">4.7. Tìm ƯCLN của mọi phần tử trong mảng.</a><br /><br /><a href="https://dataonline.io.vn/#CD4ML48">4.8. Tìm BCNN của mọi phần tử trong mảng.</a><br /><br /><br /><a href="https://dataonline.io.vn/#ML_CD5">5. Chủ đề 5. Xâu kí tự.</a><br />Mục tiêu:<br />- Cách khai báo và sử dụng string.<br />Kiến thức:<br />- Khai báo xâu string:<br />string tên_xâu;<br />string tên_xâu = &quot;&quot;;<br />string tên_xâu = &quot;xau ki tu&quot;;<br /><a href="https://dataonline.io.vn/#ML_CD51">5.1. Nhập, xuất xâu.</a><br />Nhập xâu từ bàn phím: dùng hàm getline() như sau</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S, S1;    cout&lt;&lt;&quot;Nhap xau S = &quot;;    getline(cin,S);    cout&lt;&lt;&quot;Xau S vua nhap la: &quot;&lt;&lt;S&lt;&lt;endl;    cout&lt;&lt;&quot;Nhap xau S1 = &quot;;    getline(cin,S1,&#039;.&#039;);    cout&lt;&lt;&quot;Xau S1 vua nhap la: &quot;&lt;&lt;S1&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/emBoxNS.png" alt="" /><br />Nhập xuất xâu từ tệp:&nbsp;dùng hàm getline() như sau.</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream fin;    ofstream fout;    fin.open(&quot;XAU.INP&quot;,ios::in);    fout.open(&quot;XAU.OUT&quot;,ios::out);    if(fin.fail())    {        fout&lt;&lt;&quot;Loi doc tep&quot;;    }    else    {        string hoten, diachi;        getline(fin,hoten);        getline(fin,diachi);        fout&lt;&lt;hoten&lt;&lt;endl;        fout&lt;&lt;diachi;    }    fin.close();    fout.close();    return 0;}</code></pre><p><br /><img src="https://i.imgur.com/7GbhbeU.png" alt="" /><br /><br /><a href="https://dataonline.io.vn/#ML_CD52">5.2. Các thao tác cơ bản với xâu.</a><br />- Độ dài xâu dùng hàm (phương thức): length() hoặc size()</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S = &quot;Xin    chao   tat ca    cac ban   &quot;;    int do_dai_xau = S.length();    cout&lt;&lt;&quot;Do dai xau S = &quot;&lt;&lt;do_dai_xau&lt;&lt;endl;    int do_dai_xau2 = S.size();    cout&lt;&lt;&quot;Do dai xau S = &quot;&lt;&lt;do_dai_xau2&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/yWlD7uR.png" alt="" /><br />- Xâu rỗng có độ dài bằng 0. Để kiểm tra xâu rông dùng phương thức empty() trả về giá trị true khi string rỗng, ngược lại, trả về giá trị false.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;&quot;;    cout&lt;&lt;&quot;Xau S co rong hay khong?: &quot;&lt;&lt;S.empty()&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/TY6C14j.png" alt="" /><br />Chuỗi con (Substring)<br />+ xaucon = xaugoc.substr(Vị trí bắt đầu, Độ dài xâu con);<br />Vị trí bắt đầu &gt;= 0<br />Độ dài xâu con &lt;= Độ dại xâu gốc -1</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string  xaugoc = &quot;abcdef&quot;;    string xaucon;    xaucon=xaugoc.substr(0, 1);    cout &lt;&lt; xaucon &lt;&lt; endl;    xaucon=xaugoc.substr(2, 5);    cout &lt;&lt; xaucon &lt;&lt; endl;    xaucon=xaugoc.substr(0, 3);    cout &lt;&lt; xaucon &lt;&lt; endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/5TliA9F.png" alt="" /><br />Xóa sạch xâu - làm rỗng xâu: clear()</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Quyn&quot;;    cout&lt;&lt;&quot;Xau S co rong hay khong?: &quot;&lt;&lt;S.empty()&lt;&lt;endl;    S.clear();    cout&lt;&lt;&quot;Xau S co rong hay khong?: &quot;&lt;&lt;S.empty()&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/xmUPPhF.png" alt="" /><br />Truy cập phần tử trong string S: S&#91;chỉ số&#93; hoặc S.at(chỉ số); 0&lt;= chỉ số &lt;S.length()</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang Van Quyn&quot;;    int len = S.length();    int i=0;    for(i=0; i&lt;len; i++)    {        cout&lt;&lt;&quot;Ky tu: &quot;&lt;&lt;S&#91;i&#93;&lt;&lt;endl;    }    for(i=0; i&lt;len; i++)    {        cout&lt;&lt;&quot;Ky tu: &quot;&lt;&lt;S.at(i)&lt;&lt;endl;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/K7Jpzt5.png" alt="" /><br />- Xóa kí tự cuối cùng nếu xâu không rỗng: pop_back()</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang Van Quyn&quot;;    int len = S.length();    cout&lt;&lt;&quot;Do dai S &quot;&lt;&lt;len&lt;&lt;endl;    S.pop_back();    cout&lt;&lt;&quot;Do dai S &quot;&lt;&lt;S.length()&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/HhDL1Qu.png" alt="" /><br />–&nbsp;Xóa một kí tự ở vị trí nào đó trong xâu S:&nbsp;&nbsp;S.erase(S.begin() + vị trí kí tự muốn xóa); 0&lt;=&nbsp;vị trí kí tự muốn xóa &lt;&nbsp;S.length()</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang Van Quyn&quot;;    int len = S.length();    cout&lt;&lt;S&lt;&lt;endl;    S.erase(S.begin() + 2);    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/AuptaMQ.png" alt="" /><br />Input: Vang Van Quyn<br />Output: Vag Van Quyn<br />–&nbsp;Xóa các kí tự trong một phạm vi chỉ định trong xâu S tính từ vị trí đầu: S.erase(S.begin() + start, S.begin() + end);&nbsp;0&lt;=&nbsp;start &lt;&nbsp;end&nbsp;&lt;&nbsp; S.length().&nbsp;Các kí tự bị xóa bắt đầu từ kí tự tại vị trí start đến trước vị trí end. Vậy số lượng kí tự bị xóa = end –&nbsp;start.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang Van Quyn&quot;;    int len = S.length();    cout&lt;&lt;S&lt;&lt;endl;    S.erase(S.begin() + 0, S.begin() + 3 );    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/jQjgbhV.png" alt="" /><br />Input: Vang Van Quyn<br />Output: g Van Quyn<br />–&nbsp;Xóa các kí tự trong một phạm vi chỉ định trong xâu S: S.erase(index, lengt);&nbsp;0 &lt;= index&nbsp;– là vị trí của kí tự bắt đầu xóa;lengt –&nbsp;là số lượng các kí tự sẽ xóa. Nếu&nbsp;lengt &gt;= độ dài xâu thì xóa từ vị trí index đến hết.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang        Van Quyn&quot;;    int len = S.length();    cout&lt;&lt;S&lt;&lt;endl;    S.erase(4,7);    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/EILv707.png" alt="" /><br />Input: Vang &nbsp; &nbsp; &nbsp; &nbsp;Van Quyn<br />Output: Vang Van Quyn<br />–&nbsp;Xóa các kí tự từ vị trí chỉ định trong xâu S: S.erase(index);&nbsp;0 &lt;= index&nbsp;– là vị trí của kí tự bắt đầu xóa đến cuối xâu.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Vang        Van Quyn&quot;;    int len = S.length();    cout&lt;&lt;S&lt;&lt;endl;    S.erase(4);    cout&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/4oXeMQt.png" alt="" /><br />Input: Vang &nbsp; &nbsp; &nbsp; &nbsp;Van Quyn<br />Output: Vang<br />- Nối xâu S: push_back(&#039;kí tự&#039;) nối kí tự vào cuối xâu, append(&quot;xâu kí tự&quot;), phép +</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Quy&quot;;    S.push_back(&#039;n&#039;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    S.clear();    S.append(&quot;Vang &quot;);    S.append(&quot;Van &quot;);    S.append(&quot;Quyn&quot;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    S.clear();    S=S+&quot;Vang &quot;;    S=S+&quot;Van &quot;;    S=S+&quot;Quyn&quot;;    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/V6v8PhP.png" alt="" /><br />- Chèn một xâu vào vị trí bất kì trong xâu khác: insert (vị trí chèn, xâu chèn vào); 0&lt;= vị trí chèn&lt;= độ dài xâu</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Quyn&quot;;    S.insert(1,&quot;C++&quot;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/IJWhFgl.png" alt="" /><br />- Thay thế một phần của xâu: replace (vị trí thay thế, số lượng kí tự thay thế, &nbsp;xâu sẽ thay vào);<br />0&lt;= Vị trí thay thế &lt;= S.length()</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Hoc lap trinh Pascal&quot;;    S.replace(14, 6, &quot;C++&quot;);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;S&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/ZeSdxAt.png" alt="" /><br />- Tìm vị trí xuất hiện của S1 trong S2; S2.find(S1); Nếu thấy thì trả về vị trí xuất hiện của S1 trong S2, ngược lại trả về -1.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;Hoc lap trinh Pascal&quot;;    string S1=&quot;Pascal&quot;;    int vts1=S.find(S1);    cout&lt;&lt;&quot;S la: &quot;&lt;&lt;vts1&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/mdKkODc.png" alt="" /><br />- So sanh hai xâu S1 và S2: S1.compare(S2)<br />S1.compare(S2) == 0 &nbsp; &nbsp; &nbsp; nếu &nbsp;S1== S2<br />S1.compare(S2) &gt; 0 &nbsp; &nbsp; &nbsp; &nbsp; nếu &nbsp;S1&gt; S2<br />S1.compare(S2) &lt; 0 &nbsp; &nbsp; &nbsp; &nbsp; nếu S1&lt; S2<br /><br /><a href="https://dataonline.io.vn/#ML_CD53">5.3. Chuyển đổi số kiểu số sang xâu và ngược lại.</a><br />&nbsp;- Chuyển đổi số sang xâu và ngược lại</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int songuyen=45.786;    string ssonguyen=to_string(songuyen);    int so=stoi(ssonguyen);    long so1=stol(ssonguyen);    float so2=stof(ssonguyen);    double so3=stod(ssonguyen);    cout&lt;&lt;songuyen&lt;&lt;endl;    cout&lt;&lt;so&lt;&lt;endl;    cout&lt;&lt;so1&lt;&lt;endl;    cout&lt;&lt;so2&lt;&lt;endl;    cout&lt;&lt;so3&lt;&lt;endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/A9Uii1e.png" alt="" /><br />- Chuyển đổi chữ số sang con số và ngược lại.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    char ch =&#039;4&#039;;    int so =ch-&#039;0&#039;;    cout&lt;&lt;so&lt;&lt;endl;    so =ch-48;    cout&lt;&lt;so&lt;&lt;endl;    cout&lt;&lt;so+5;    return 0;}</code></pre><p><img src="https://i.imgur.com/Y1GEq6w.png" alt="" /><br /><br /><br /><a href="https://dataonline.io.vn/#ML_CD54">5.4. Kiểm tra xem một kí tự trong xâu có phài là chữ số hay không?</a><br />&nbsp;- Hàm isdigit kiểm tra xem một kí tự có phải là chữ số hay không?</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S = &quot;abh45gghu6f8j7k9lpn&quot;;    int i =0, len=S.length();    for(i=0; i&lt;len; i++)    {        cout&lt;&lt;isdigit(S.at(i));    }    return 0;}</code></pre><p><img src="https://i.imgur.com/mPkbSUv.png" alt="" /></p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    int so =4;    char ch =so+&#039;0&#039;;    cout&lt;&lt;ch&lt;&lt;endl;    ch =so+48;    cout&lt;&lt;ch&lt;&lt;endl;    cout&lt;&lt;ch+5&lt;&lt;endl;    cout&lt;&lt;(ch+5)-&#039;0&#039;;    return 0;}</code></pre><p><img src="https://i.imgur.com/QA6rX5w.png" alt="" /><br />- Bài tập áp dụng, cho xâu S= &quot;abh45gghu6f82j7k9lpn”. Chuyển các kí tự là chữ số sang kiểu số và tính tổng các số đó (tức là tính 4+5+6+8+2+7+9)</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S = &quot;abh45gghu6f82j7k9lpn&quot;;    int i =0, len=S.length();    int T=0;    for(i=0; i&lt;len; i++)    {        if(isdigit(S.at(i)))        {            int so =(int)(S.at(i)-&#039;0&#039;);            cout&lt;&lt;so&lt;&lt;endl;            T=T+so;        }    }    cout&lt;&lt;&quot;Tong = &quot;&lt;&lt;T;    return 0;}</code></pre><p><img src="https://i.imgur.com/lH9PfZM.png" alt="" /><br /><img src="https://i.imgur.com/lNAs5ww.png" alt="" /><br /><br />Bài tập vận dụng,&nbsp;cho xâu S= &quot;abh45gghu6f82j7k9lpn”. Tính tổng các số đó là các chữ số liên tiếp nhau trong xâu S được chuyển sang kiểu số (tức là tính 45+6+82+7+9)</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S = &quot;abh45gghu6f82j7k9lpn&quot;;    int i =0, len=S.length();    int T=0;    int so =0;    string chuso=&quot;&quot;;    for(i=0; i&lt;len; i++)    {        if(isdigit(S.at(i)))        {            so=so*10+S.at(i)-&#039;0&#039;;        }        else        {            if(so!=0)            {                cout&lt;&lt;so&lt;&lt;endl;                T=T+so;            }            so=0;        }    }    cout&lt;&lt;&quot;Tong = &quot;&lt;&lt;T;    return 0;}</code></pre><p><img src="https://i.imgur.com/uyHkF24.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_CD55">5.5. Chuyển đổi chữ HOA - chữ thường.</a><br />* Chuyển chữ HOA sang chữ thường:&nbsp;tolower(kí tự); Kiểm tra xem có phải chữ HOA hay không: isupper(kí tự)</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string str=&quot;VAnG vAN qUyN&quot;;    int len = str.length();    int i=0, c;    for (i = 0; i &lt; len; i++)    {        c = str&#91;i&#93;;        if (isupper(c))        {            str&#91;i&#93; = tolower(c);        }    }    cout&lt;&lt;str;    return 0;}</code></pre><p><img src="https://i.imgur.com/SMGTAdR.png" alt="" /><br />* Chuyển chữ thường sang chữ HOA:&nbsp;toupper(kí tự); Kiểm tra xem có phải chữ thường hay không:&nbsp;islower(kí tự);</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string str=&quot;VAnG vAN qUyN&quot;;    int len = str.length();    int i=0, c;    for (i = 0; i &lt; len; i++)    {        c = str&#91;i&#93;;        if (islower(c))        {            str&#91;i&#93; = toupper(c);        }    }    cout&lt;&lt;str;    return 0;}</code></pre><p><img src="https://i.imgur.com/XPaaZ01.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_CD56">5.6. Chuẩn hóa xâu.</a><br /><a href="https://dataonline.io.vn/#ML_CD561">5.6.1. Xóa kí tự dấu cách thừa trong xâu.</a><br />* Xóa kí tự dấu cách thừa gồm: Xóa dấu cách ở đầu xâu, cuối xâu, dấu cách thừa giữa hai từ có từ hai dấu cách trở lên.<br />Ví dụ 1 dùng phương thức find xóa dấu cách thừa giữa hai từ tương tự như hàm POS trong Pascal.</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;      chao    cac     ban      &quot;;    cout &lt;&lt; S &lt;&lt; endl;    while(S&#91;0&#93;==&#039; &#039;)    {        S.erase(0,1);    }    cout &lt;&lt; S &lt;&lt; endl;    while(S&#91;S.length()&#93;==&#039; &#039;)    {        S.erase(S.length()-1,1);    }    cout &lt;&lt; S &lt;&lt; endl;    while(S.find(&quot;  &quot;) != -1)    {        S.erase(S.find(&quot;  &quot;),1);    }    cout &lt;&lt; S &lt;&lt; endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/hTHPn54.png" alt="" /><br />Ví dụ 2 dùng phương thức find xóa dấu cách thừa giữa hai từ (nếu kí tự thứ i và i+1 đều là dấu cách thì xóa một kí tự).</p><pre><code class="language-cpp">#include &lt;iostream&gt;using namespace std;int main(){    string S=&quot;      chao    cac     ban      &quot;;    cout &lt;&lt; S &lt;&lt; endl;    while(S&#91;0&#93;==&#039; &#039;)    {        S.erase(0,1);    }    cout &lt;&lt; S &lt;&lt; endl;    while(S&#91;S.length()&#93;==&#039; &#039;)    {        S.erase(S.length()-1,1);    }    cout &lt;&lt; S &lt;&lt; endl;    int dodai = S.length(), i = 0;    for(i = 0; i &lt; dodai; i++)    {        if((S&#91;i&#93;==&#039; &#039;)&amp;&amp;(S&#91;i+1&#93;==&#039; &#039;))        {            S.erase(i,1);            dodai--;            i--;        }    }    cout &lt;&lt; S &lt;&lt; endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/Ccdov8S.png" alt="" /><br />Ví dụ 3 dùng&nbsp;stringstream trong C++</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;sstream&gt;using namespace std;int main(){    string kq;    string S = &quot;xin     chao    tat     ca cac ban&quot;;    stringstream ss(S);    string token = &quot; &quot;;    while (ss &gt;&gt; token)    {        kq.append(token).append(&quot; &quot;);    }    kq.pop_back();   // kq.replace(kq.length()-1,1,&quot;&quot;);    S=kq;    kq.clear();    cout &lt;&lt; S &lt;&lt; endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/4jUnMXb.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_CD562">5.6.2. Chuẩn hóa danh từ riêng.</a></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;sstream&gt;using namespace std;int main(){    string kq;    string S = &quot;xin     chao    tat     ca cac ban&quot;;    stringstream ss(S);    string token = &quot; &quot;;    char ch;    int i =0, len =0;    while (ss &gt;&gt; token)    {        len=tach.length();        for(i=1; i&lt;len; i++)        {            ch = tach.at(i);            tach.at(i)=tolower(ch);        }        ch = token.at(0);        token.at(0) = toupper(ch);        kq.append(token).append(&quot; &quot;);    }    kq.pop_back();    //kq.replace(kq.length()-1,1,&quot;&quot;);    S=kq;    kq.clear();    cout &lt;&lt; S &lt;&lt; endl;    return 0;}</code></pre><p><img src="https://i.imgur.com/jDxo2Rq.png" alt="" /><br /><a href="https://dataonline.io.vn/#ML_563"><strong>5.6.3. Sắp xếp các kí tự trong xâu tăng/giảm bằng hàm sort (thư viện:&nbsp;#include&lt;algorithm&gt;)</strong></a><br />code:</p><pre><code class="language-cpp">#include &lt;iostream&gt;
#include&lt;algorithm&gt;

using namespace std;

int main()
{
    string s1=&quot;5450&quot;;//&quot;Abc0gh05hh45k0*&quot;;
    string s2=&quot;708&quot;;//*/def0hh070b8k’&quot;;

    sort(s1.begin(), s1.end());
    cout &lt;&lt;&quot;Sap xep tang dan: &quot;&lt;&lt;s1&lt;&lt; endl;

    sort(s1.begin(), s1.end(),greater&lt;char&gt;());
    cout &lt;&lt;&quot;Sap xep giam dan: &quot;&lt;&lt; s1&lt;&lt; endl;

    sort(s2.begin(), s2.end(),greater&lt;char&gt;());
    cout &lt;&lt;&quot;Sap xep giam dan: &quot;&lt;&lt; s2&lt;&lt; endl;

    string s3=s1+s2;
    cout &lt;&lt;&quot;Noi s2 vao s1 thanh s3: &quot;&lt;&lt; s3&lt;&lt; endl;

    sort(s3.begin(), s3.end(),greater&lt;char&gt;());
    cout &lt;&lt;&quot;Sap xep s3 giam dan de tao duoc so lon nhat: &quot;&lt;&lt; s3&lt;&lt; endl;

    return 0;
}
</code></pre><p>Minh họa:<br /><img src="https://i.imgur.com/IYhEXCv.jpeg" alt="" /><br /><br /><a href="https://dataonline.io.vn/#ML_CD7">&nbsp;6. Vector.</a><br />6.1. Khái niệm: Là một mảng động có các phần tử nằm liên tiếp nhau trong bộ nhớ nhưng có thể thay đổi kích thước bất kì.<br />6.2. Khai báo.<br />Dùng thư viện chuẩn:&nbsp; #include &lt;vector&gt;<br />Có 2 cách khởi tạo vector là:&nbsp;<br />vector&lt;&quot;kiểu dữ liệu&quot;&gt; &quot;tên vector&quot;;<br />vector&lt;&quot;kiểu dữ liệu&quot;&gt; &quot;tên vector&quot;(số lượng phần tử);<br />6.3. Nhập xuất.<br />Nhập xuất vector giống với nhập xuất mảng nhưng ta có thêm hàm/phương thức bổ trợ để thêm giá trị vào cuối vector trong trường hợp chưa biết rõ vector có bao nhiêu phần tử hoặc trong lúc thực thi ta muốn thêm giá trị vào là: &quot;tên vector&quot;.push_back(giá trị); Xem trong phần ví dụ.<br />6.4. Các hàm/phương thức.<br />myvector.push_back(x);&nbsp;Để thêm một phần tử x vào vị trí sau cùng của vector:<br />myvector.insert(pos, x); thêm x vào vector tại vị trí pos<br />myvector.insert(pos, k, x); thêm k phần tử x vào vector từ vị trí pos<br />myvector.insert(pos, x.begin(), x.end()); thêm vector x vào vector myvector từ vị trí pos<br />myvector.insert(pos, array, array + k); thêm k phần tử trong mảng array vào vector từ vị trí pos<br />Ở đây ta lưu ý phương thức begin() là trả về vị trí đầu của vector và end() là trả về &nbsp;vị trí cuối của vector<br /><span style="color:rgb(39,174,96);">myvector.erase(myvector.begin()+pos)</span>; xóa phần tử tại vị trí pos trong vector;<br />myvector.erase(myvector.begin()+i; myvector.begin()+k); xóa các phần tử từ vị trí i cho tới vị trí k<br />myvector.erase(i, k); //xóa các phần tử từ vị trí i cho tới vị trí k<br />myvector.pop_back(); xóa phần tử cuối cùng của vector<br />sort(v.begin(), v.end()); Sắp xếp tăng dần toàn bộ<br />sort(v.begin(), v.end(), greater&lt;int&gt;()); Sắp xếp giảm dần toàn bộ<br />sort(first, last); // sắp xếp từ vị trí first tới last tăng dần<br />sort(first, last, greater&lt;int&gt;()); // sắp xếp từ vị trí first tới last giảm dần thêm&nbsp;#include &lt;algorithm&gt;&nbsp;<br />Lấy số lượng phần tử:&nbsp;myvector.size();<br />myvector.clear(); //xóa tất cả phần tử trong vector<br />Vị trí đầu: myvector.begin() hoặc&nbsp;myvector.fornt();<br />Vị trí cuối: myvector.end() // hoặc myvector.begin() + myvector.size() hoặc&nbsp;myvector.back();<br />myvector&#91;pos&#93;&nbsp; hoặc myvector.at(pos); // truy cập trực tiếp/lấy&nbsp;&nbsp;phần tử ở vị trí&nbsp;pos (tính từ 0)<br />Kiểm tra vector giỗng:&nbsp;myvector.empty();<br />reverse(myvector.begin(), myvector.end()); &nbsp;//đảo ngược cả vector&nbsp;<br />reverse(myvector.begin(), myvector.begin()+2); //đảo ngược từ vị trí đầu tới vị trí đầu +2;<br />Đổi 2 vector:&nbsp;swap(vta, vtb); //tất cả các phần tử của vta được gán cho vtb và ngược lại<br />ten-vector.rbegin();&nbsp; Đảo ngược các phần tử từ cuối trở về đầu<br />ten-vector.rend();&nbsp; Đảo ngược các phần tử từ đầu trở về cuối<br />vector.max_size();&nbsp;hàm trả về số phần tử tối đa mà vector có thể chứa<br />vector.capacity(); hàm trả về số phần tử được cấp phát cho vector nằm trong bộ nhớ<br />vector.reserve(n);&nbsp;hàm cấp cho vector số dung lượng vừa đủ để chứa n phần tử<br />6.5. Ví dụ..<br />6.5.1. Ví dụ nhập giá trị cho vector từ bàn phím dùng phương thức&nbsp;push_back khi không biết vector có bao nhiêu phần tử.</p><pre><code class="language-cpp">
#include &lt;iostream&gt;#include &lt;vector&gt;using namespace std;int main(){    vector&lt;int&gt; v;    int i, giatri;    for (i=0; i&lt;10; i++)    {        cout&lt;&lt;&quot;Nhap phan tu thu &quot;&lt;&lt;i&lt;&lt;&quot;: &quot;;        cin&gt;&gt;giatri;        v.push_back(giatri);    }    cout&lt;&lt;v.size()&lt;&lt;endl;    v.push_back(58);    int soluong=v.size();    cout&lt;&lt;soluong&lt;&lt;endl;    for (i=0; i&lt;soluong; i++)    {        cout&lt;&lt;        v&#91;i&#93;&lt;&lt;&quot; &quot;;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/Pg07TJf.png" alt="" /><br />6.5.2. Ví dụ nhập giá trị cho vector từ bàn phím khi biết vector có bao nhiêu phần tử, dùng tương tự như mảng.</p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;vector&gt;using namespace std;int main(){    int i, n;    cout&lt;&lt;&quot;Nhap so phan cua day, n= &quot;;    cin&gt;&gt;n;    vector&lt;int&gt; v(n);    for (i=0; i&lt;n; i++)    {        cout&lt;&lt;&quot;Nhap phan tu thu &quot;&lt;&lt;i&lt;&lt;&quot;: &quot;;        cin&gt;&gt;v&#91;i&#93;;    }    cout&lt;&lt;v.size()&lt;&lt;endl;    v.push_back(58);    int soluong=v.size();    cout&lt;&lt;soluong&lt;&lt;endl;    for (i=0; i&lt;soluong; i++)    {        cout&lt;&lt;v&#91;i&#93;&lt;&lt;&quot; &quot;;    }    return 0;}</code></pre><p><img src="https://i.imgur.com/aVm8QLO.png" alt="" /><br />6.5.1. Ví dụ nhập giá trị cho vector đọc từ tệp dùng phương thức&nbsp;push_back khi không biết vector có bao nhiêu phần tử.<br /><img src="https://i.imgur.com/wWivDhH.png" alt="" width="530" height="165" /></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;vector&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream fin;    ofstream fout;    fin.open(&quot;DEMO.INP&quot;,ios::in);    fout.open(&quot;DEMO.OUT&quot;,ios::out);    if(fin.fail())    {        fout&lt;&lt;&quot;Loi doc tep&quot;;    }    else    {        vector&lt;long long&gt; v;        long long i, n, giatri;        fin&gt;&gt;n;        for (i=0; i&lt;n; i++)        {            fin&gt;&gt;giatri;            v.push_back(giatri);        }        long long soluong=v.size();        for (i=0; i&lt;soluong; i++)        {            fout&lt;&lt;v&#91;i&#93;&lt;&lt;&quot; &quot;;        }    }    fin.close();    fout.close();    return 0;}</code></pre><p><img src="https://i.imgur.com/yqu8fxT.png" alt="" /><br />* Ví dụ.<br /><img src="https://i.imgur.com/r0rqPPC.png" alt="" /></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;vector&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream fin;    ofstream fout;    fin.open(&quot;DEMO.INP&quot;,ios::in);    fout.open(&quot;DEMO.OUT&quot;,ios::out);    if(fin.fail())    {        fout&lt;&lt;&quot;Loi doc tep&quot;;    }    else    {        vector&lt;long long&gt; v;        long long i,giatri;        while(!fin.eof())        {            fin&gt;&gt;giatri;            v.push_back(giatri);        }        long long soluong=v.size();        for (i=0; i&lt;soluong; i++)        {            fout&lt;&lt;v&#91;i&#93;&lt;&lt;&quot; &quot;;        }    }    fin.close();    fout.close();    return 0;}</code></pre><p><img src="https://i.imgur.com/i9fllnw.png" alt="" /><br />6.5.2. Ví dụ nhập giá trị cho vector đọc từ tệ&nbsp; khi biết vector có bao nhiêu phần tử, dùng tương tự như mảng.<br /><img src="https://i.imgur.com/wWivDhH.png" alt="" /></p><pre><code class="language-cpp">#include &lt;iostream&gt;#include &lt;vector&gt;#include &lt;fstream&gt;using namespace std;int main(){    ifstream fin;    ofstream fout;    fin.open(&quot;DEMO.INP&quot;,ios::in);    fout.open(&quot;DEMO.OUT&quot;,ios::out);    if(fin.fail())    {        fout&lt;&lt;&quot;Loi doc tep&quot;;    }    else    {        long long i, n;        fin&gt;&gt;n;        vector&lt;long long&gt; v(n);        for (i=0; i&lt;n; i++)        {            fin&gt;&gt;v&#91;i&#93;;        }        long long soluong=v.size();        for (i=0; i&lt;soluong; i++)        {            fout&lt;&lt;v&#91;i&#93;&lt;&lt;&quot; &quot;;        }    }    fin.close();    fout.close();    return 0;}</code></pre><p><img src="https://i.imgur.com/3BDGaLH.png" alt="" /><br />&nbsp;</p>
		</div>
				<div id="author">
						<p>
				<strong>Tác giả:</strong>
				<a href="https://dataonline.io.vn/author/Vang-Van-Quyn/">Vàng Văn Quyn</a>
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v2-240.html" title="Tài liệu C++ và CodeBlocks V2">https://365.io.vn/savefile/tai-lieu-hoc-lap-trinh/tai-lieu-c-va-codeblocks-v2-240.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; DATAONLINE.IO.VN
		</div>
		<div id="contact">
			<a href="mailto:support@dataonline.io.vn">support@dataonline.io.vn</a>
		</div>
	</div>
</div>
        <div class="cookie-notice"><div><button onclick="cookie_notice_hide();">&times;</button>Chúng tôi đang sử dụng cookie để cung cấp cho bạn những trải nghiệm tốt nhất trên trang web này. Bằng cách tiếp tục truy cập, bạn đồng ý với <a href="https://dataonline.io.vn/siteterms/privacy.html">Chính sách thu thập và sử dụng cookie</a> của chúng tôi.</div></div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://dataonline.io.vn/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
<script src="https://dataonline.io.vn/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://dataonline.io.vn/assets/js/language/vi.js"></script>
<script src="https://dataonline.io.vn/assets/js/DOMPurify/purify3.js"></script>
<script src="https://dataonline.io.vn/assets/js/global.js"></script>
<script src="https://dataonline.io.vn/assets/js/site.js"></script>
<script src="https://dataonline.io.vn/themes/default/js/news.js"></script>
<script src="https://dataonline.io.vn/themes/default/js/main.js"></script>
<script src="https://dataonline.io.vn/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://dataonline.io.vn",
            "logo": "https://dataonline.io.vn/assets/images/logo.svg"
        }
        </script>
<script src="https://dataonline.io.vn/themes/default/js/bootstrap.min.js">
        </script>
<script>
    var LIMIT = 600000; // 10 phút

    var startTime = sessionStorage.getItem("redirect_timer");
    if (!startTime) {
        startTime = Date.now();
        sessionStorage.setItem("redirect_timer", startTime);
    } else {
        startTime = parseInt(startTime, 10);
    }

    var elapsed = Date.now() - startTime;
    var remain = LIMIT - elapsed;

    if (remain > 0) {
        setTimeout(function () {
            alert("Hệ thống sẽ chuyển sang trang chấm bài.");
            window.location.replace("https://vnoj.io.vn");
        }, remain);
    } else {
        window.location.replace("https://vnoj.io.vn");
    }
</script>
</body>
</html>