This commit is contained in:
Nathan Wang 2020-07-14 23:19:45 -07:00
parent 5942cd52ff
commit 048894d7d0
4 changed files with 78 additions and 19 deletions

View file

@ -1,18 +1,43 @@
---
id: pairs-tuples
title: Pairs & Tuples in C++
title: Pairs & Tuples
author: Benjamin Qi, Nathan Wang, Darren Yao, Abutalib Namazov
description: "Introduction to pairs, which allow you to store two objects (possibly of different types) as a single unit, as well as tuples, which are a generalization of pairs."
prerequisites:
- Bronze - Introduction to Data Structures
---
<LanguageSection>
<CPPSection />
<PySection>
<warning-block title="Language Note">
**Pairs are not available in Python.** Just use tuples; no need for pairs!
</warning-block>
</PySection>
<JavaSection>
<warning-block title="Language Note">
**Pairs and tuples are not available in Java.** If you are using Java, just skip this module.
</warning-block>
</JavaSection>
</LanguageSection>
Storing multiple elements as a single unit allows you to change the order of the elements (e.g. sorting) without mixing one component with another.
**Example:** Given some points on a plane where the $i$-th point has coordinates $(x_i,y_i)$, sort these points in increasing order of $x$ coordinate and increasing order of $y$ in case there is a tie.
If you sort one of $x_i$ and $y_i$ without considering the other, the points will not be in the correct order. We need to consider the pair $(x_i,y_i)$ as a single unit.
<LanguageSection>
<CPPSection>
## [Pairs](http://www.cplusplus.com/reference/utility/pair/pair/)
- `pair<type1, type2> p`: Creates a pair `p` with two elements with the first one being of `type1` and the second one being of `type2`.
@ -45,6 +70,7 @@ int main() {
*/
```
## [Tuples](http://www.cplusplus.com/reference/tuple/)
Of course, we can hold more than two values with something like `pair<int,pair<int,int>>`, but it gets messy when you need a lot of elements. In this case, using **tuples** might be more convenient.
@ -122,4 +148,30 @@ int main() {
*/
```
Tuples are sorted similarly.
Tuples are sorted similarly.
</CPPSection>
<JavaSection>
**Pairs and Tuples are not available in Java. Skip this module if you're a Java user!**
</JavaSection>
<PySection>
## Python Tuples
Use the link below (if you know of a better one, please let us know!) to learn about tuples.
Python automatically sorts a list of tuples by the first element, then the second, and so on. This can save you time and keystrokes for certain problems like the one above.
<resources>
<resource source="Tutorialspoint" title="Python Tuples" url="https://www.tutorialspoint.com/python/python_tuples.htm" starred />
</resources>
</PySection>
</LanguageSection>

View file

@ -38,7 +38,7 @@ Both Java and C++ contain two versions of sets and maps; one in which the keys a
(more in-depth explanation?)
## HashSets
## Unordered Sets (HashSets)
<LanguageSection>
@ -95,7 +95,7 @@ for(int element : set){
</LanguageSection>
## HashMaps
## Unordered Maps (HashMaps)
<problems-list problems={metadata.problems.ex} />
@ -140,7 +140,9 @@ System.out.println(map.containsKey(1)); // true
</LanguageSection>
## Hacking
## Hacking (Optional)
_You don't need to know this for USACO, so you can safely skip this section._
In USACO contests, unordered sets and maps generally fine, but the built-in hashing algorithm for C++ is vulnerable to pathological data sets causing abnormally slow runtimes. Apparently [Java](https://codeforces.com/blog/entry/62393?#comment-464875) is not vulnerable to this, however.
@ -174,11 +176,8 @@ unordered_map<int,int,chash> U;
</CPPSection>
<JavaSection>
</JavaSection>
<JavaSection />
<PySection />
</LanguageSection>

View file

@ -49,7 +49,7 @@ const components = {
</div>
),
'warning-block': ({ children, title }) => (
<div className="rounded-md bg-yellow-50 p-4">
<div className="rounded-md bg-yellow-50 p-4 mb-4">
<div className="flex">
<div className="flex-shrink-0">
<svg

View file

@ -1,6 +1,5 @@
import { createContext } from 'react';
import { createContext, useRef, useState } from 'react';
import * as React from 'react';
import useStickyState from '../hooks/useStickyState';
const UserSettingsContext = createContext({
primaryLang: 'showAll',
@ -8,13 +7,22 @@ const UserSettingsContext = createContext({
});
export const UserSettingsProvider = ({ children }) => {
const [primaryLang, setPrimaryLang] = useStickyState(
'showAll',
'user-primary-lang'
);
const key = 'guide:userSettings:primaryLang';
const [primaryLang, setPrimaryLang] = useState('showAll');
const initialRender = useRef(true);
React.useEffect(() => {
if (primaryLang === 'showAll') setPrimaryLang('cpp');
}, []);
if (initialRender.current) {
const stickyValue = window.localStorage.getItem(key);
if (stickyValue !== null) setPrimaryLang(JSON.parse(stickyValue));
else setPrimaryLang('C++');
initialRender.current = false;
} else if (primaryLang !== 'showAll') {
window.localStorage.setItem(key, JSON.stringify(primaryLang));
}
}, [key, primaryLang]);
return (
<UserSettingsContext.Provider
value={{