#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v{ 2 ,3 , 5, 1, 4 };
sort(v.begin(), v.end());
for (auto i : v) cout << i << ", ";
cout << endl;
int a[] = { 2,3,5,1,4 };
sort(begin(a), end(a));
for (auto i : a) cout << i << ",";
cout << endl;
}
カテゴリー: programming
React リスト項目にkey属性を設定する
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const Menu = (props) => {
return (
<li>
<button>-</button>
<button>+</button>
{props.name}({props.price}G X 0個)
</li>
);
};
const App = () => {
const menus = [
{id: 0,name: '聖剣', price: 400},
{id: 1,name: '魔装銃', price: 500},
{id: 2,name: '魔剣', price: 300},
];
const menuItems = menus.map((menu) =>{
return(
<Menu
key={menu.id}
name={menu.name}
price={menu.price}
/>
);
});
return (
<>
<h1>メニュー</h1>
<ul className="menus">
{menuItems}
</ul>
<p>合計: 0円</p>
</>
);
};
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(<App />);
}
</script>
</body>
</html>
style.css
@charset "utf-8";
body{
margin: 0;
}
#container{
width: 400px;
margin: auto;
}
h1{
margin: 16px 0 0 0;
font-size: 20px;
text-align: center;
}
.menus{
margin: 0;
padding: 0;
list-style-type: none;
}
.menus > li{
border: 1px solid #ccc;
padding: 8px;
border-radius: 8px;
margin-top: 16px;
}
.menus button{
margin-right: 8px;
width: 24px;
}
p{
margin: 0;
text-align: right;
}
React 配列
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const Menu = (props) => {
return (
<li>
<button>-</button>
<button>+</button>
{props.name}({props.price}G X 0個)
</li>
);
};
const App = () => {
const menus = [
{name: '聖剣', price: 400},
{name: '魔装銃', price: 500},
{name: '魔剣', price: 300},
];
const menuItems = menus.map((menu) =>{
return(
<Menu
name={menu.name}
price={menu.price}
/>
);
});
return (
<>
<h1>メニュー</h1>
<ul className="menus">
{menuItems}
</ul>
<p>合計: 0円</p>
</>
);
};
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(<App />);
}
</script>
</body>
</html>
style.css
@charset "utf-8";
body{
margin: 0;
}
#container{
width: 400px;
margin: auto;
}
h1{
margin: 16px 0 0 0;
font-size: 20px;
text-align: center;
}
.menus{
margin: 0;
padding: 0;
list-style-type: none;
}
.menus > li{
border: 1px solid #ccc;
padding: 8px;
border-radius: 8px;
margin-top: 16px;
}
.menus button{
margin-right: 8px;
width: 24px;
}
p{
margin: 0;
text-align: right;
}
React Appコンポーネント
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<link rel="stylesheet" href="style.css">
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const App = () => {
return(
<>
<h1>アイテム</h1>
<ul className="menus">
<li>ポーション</li>
<li>エリクサー</li>
<li>ラストエリクサー</li>
</ul>
<p>合計: 0G</p>
</>
);
};
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(<App/>);
}
</script>
</body>
</html>
style.css
@charset "utf-8";
body{
margin: 0;
}
#container{
width: 400px;
margin: auto;
}
h1{
margin: 16px 0 0 0;
font-size: 20px;
text-align: center;
}
.menus{
margin: 0;
padding: 0;
list-style-type: none;
}
.menus > li{
border: 1px solid #ccc;
padding: 8px;
border-radius: 8px;
margin-top: 16px;
}
p{
margin: 0;
text-align: right;
}
React CSSでスタイリングしていこう
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<link rel="stylesheet" href="style.css">
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(
<>
<h1>アイテム</h1>
<ul>
<li>ポーション</li>
<li>エリクサー</li>
<li>ラストエリクサー</li>
</ul>
<p>合計: 0G</p>
</>
);
}
</script>
</body>
</html>
style.css
@charset "utf-8";
body{
margin: 0;
}
#container{
width: 400px;
margin: auto;
background: pink;
}
h1{
margin: 0;
font-size: 20px;
text-align: center;
}
ul{
margin: 0;
padding: 0;
list-style-type: none;
}
p{
margin: 0;
text-align: right;
}
React JSX記法
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My React App</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/babel">
'use strict';
{
const container = document.querySelector('#container');
const root = ReactDOM.createRoot(container);
root.render(
<>
<h1>アイテム</h1>
<ul>
<li>ポーション</li>
<li>エリクサー</li>
<li>ラストエリクサー</li>
</ul>
<p>合計: 0G</p>
</>
);
}
</script>
</body>
</html>
tailwindcss To-Do List
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 flex items-center justify-center h-screen">
<div class="bg-white p-8 rounded-lg shadow-lg w-full max-w-md">
<h1 class="text-2xl font-bold mb-4">To-Do List</h1>
<form id="todo-form" class="flex mb-4">
<input id="todo-input" type="text" placeholder="Add a new task" class="flex-grow p-2 border rounded-l-lg focus:outline-none">
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-r-lg hover:bg-blue-700">Add</button>
</form>
<div class="mb-4">
<button id="filter-all" class="filter-btn bg-gray-200 text-black px-4 py-2 rounded-l-lg">All</button>
<button id="filter-active" class="filter-btn bg-gray-200 text-black px-4 py-2">Active</button>
<button id="filter-completed" class="filter-btn bg-gray-200 text-black px-4 py-2 rounded-r-lg">Completed</button>
</div>
<ul id="todo-list" class="list-disc pl-5">
<!-- To-Do items will be added here -->
</ul>
</div>
<script>
const todoForm = document.getElementById('todo-form');
const todoInput = document.getElementById('todo-input');
const todoList = document.getElementById('todo-list');
const filterButtons = document.querySelectorAll('.filter-btn');
document.addEventListener('DOMContentLoaded', loadTodos);
todoForm.addEventListener('submit', function(event) {
event.preventDefault();
addTodoItem(todoInput.value);
todoInput.value = '';
});
filterButtons.forEach(button => {
button.addEventListener('click', () => {
document.querySelectorAll('.filter-btn').forEach(btn => btn.classList.remove('bg-blue-500', 'text-white'));
button.classList.add('bg-blue-500', 'text-white');
filterTodos(button.id);
});
});
function addTodoItem(task) {
if (task === '') return;
const listItem = document.createElement('li');
listItem.classList.add('flex', 'items-center', 'justify-between', 'py-2', 'border-b', 'border-gray-200');
const taskText = document.createElement('span');
taskText.textContent = task;
taskText.classList.add('flex-grow', 'cursor-pointer');
taskText.addEventListener('click', toggleComplete);
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.classList.add('bg-yellow-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-yellow-700');
editButton.addEventListener('click', () => editTodoItem(listItem, taskText));
const priorityButton = document.createElement('button');
priorityButton.textContent = 'Important';
priorityButton.classList.add('bg-green-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-green-700');
priorityButton.addEventListener('click', () => togglePriority(taskText));
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.classList.add('bg-red-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-red-700');
deleteButton.addEventListener('click', () => deleteTodoItem(listItem));
listItem.appendChild(taskText);
listItem.appendChild(editButton);
listItem.appendChild(priorityButton);
listItem.appendChild(deleteButton);
todoList.appendChild(listItem);
saveTodos();
}
function toggleComplete(event) {
event.target.classList.toggle('line-through');
event.target.classList.toggle('text-gray-500');
saveTodos();
}
function togglePriority(taskText) {
taskText.classList.toggle('font-bold');
saveTodos();
}
function editTodoItem(listItem, taskText) {
const newTask = prompt('Edit your task:', taskText.textContent);
if (newTask !== null && newTask !== '') {
taskText.textContent = newTask;
saveTodos();
}
}
function deleteTodoItem(listItem) {
listItem.remove();
saveTodos();
}
function saveTodos() {
const todos = [];
document.querySelectorAll('#todo-list li').forEach((item) => {
todos.push({
text: item.querySelector('span').textContent,
completed: item.querySelector('span').classList.contains('line-through'),
important: item.querySelector('span').classList.contains('font-bold')
});
});
localStorage.setItem('todos', JSON.stringify(todos));
}
function loadTodos() {
const savedTodos = JSON.parse(localStorage.getItem('todos')) || [];
savedTodos.forEach((todo) => {
const listItem = document.createElement('li');
listItem.classList.add('flex', 'items-center', 'justify-between', 'py-2', 'border-b', 'border-gray-200');
const taskText = document.createElement('span');
taskText.textContent = todo.text;
taskText.classList.add('flex-grow', 'cursor-pointer');
if (todo.completed) {
taskText.classList.add('line-through', 'text-gray-500');
}
if (todo.important) {
taskText.classList.add('font-bold');
}
taskText.addEventListener('click', toggleComplete);
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.classList.add('bg-yellow-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-yellow-700');
editButton.addEventListener('click', () => editTodoItem(listItem, taskText));
const priorityButton = document.createElement('button');
priorityButton.textContent = 'Important';
priorityButton.classList.add('bg-green-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-green-700');
priorityButton.addEventListener('click', () => togglePriority(taskText));
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.classList.add('bg-red-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-red-700');
deleteButton.addEventListener('click', () => deleteTodoItem(listItem));
listItem.appendChild(taskText);
listItem.appendChild(editButton);
listItem.appendChild(priorityButton);
listItem.appendChild(deleteButton);
todoList.appendChild(listItem);
});
}
function filterTodos(filter) {
const allTodos = document.querySelectorAll('#todo-list li');
allTodos.forEach((todo) => {
switch (filter) {
case 'filter-all':
todo.style.display = 'flex';
break;
case 'filter-active':
todo.querySelector('span').classList.contains('line-through') ? todo.style.display = 'none' : todo.style.display = 'flex';
break;
case 'filter-completed':
todo.querySelector('span').classList.contains('line-through') ? todo.style.display = 'flex' : todo.style.display = 'none';
break;
}
});
}
</script>
</body>
</html>
Tailwind CSS入門 インタラクティブにスタイルを変えてみよう
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<button
class="bg-orange-400 text-white m-4 py-2 px-4 rounded-full shadow-black/30 shadow-md hover:opacity-80 transition duration-500 active:translate-y-1">
Buy now!
</button>
</body>
</html>
Tailwind CSS入門 ボタンのスタイリングをしてみよう
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<button class="bg-orange-400 text-white m-4 py-2 px-4 rounded-full shadow-black/30 shadow-md">
Buy now!
</button>
</body>
</html>
Tailwind CSS入門 境界線を設定してみよう
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="bg-red-400 w-20 h-20 border-solid border-gray-200 border-8">Box 1</div>
<div class="bg-sky-400 w-20 h-20 border-dashed border-gray-200 border-b-8">Box 2</div>
</body>
</html>
