Updated: Friday, September 01,2023-09-01 08:04:15

main
shwetha729 2023-09-01 08:27:59 -04:00
parent 4d3352bdaa
commit 074b0c8807
4 changed files with 118 additions and 12 deletions

View File

@ -2,7 +2,7 @@
"recentFiles": [
{
"basename": "C++",
"path": "C++.md"
"path": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md"
},
{
"basename": "gdevelop.io",

View File

@ -65,7 +65,7 @@
"state": {
"type": "markdown",
"state": {
"file": "C++.md",
"file": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md",
"mode": "source",
"backlinks": false,
"source": false
@ -148,7 +148,7 @@
"state": {
"type": "outline",
"state": {
"file": "C++.md"
"file": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md"
}
}
},
@ -158,7 +158,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "C++.md",
"file": "Coding Tips (Classical)/Terminal Tips/Languages/C++.md",
"linksCollapsed": false,
"unlinkedCollapsed": false
}
@ -183,10 +183,10 @@
"audio-recorder:Start/stop recording": false
}
},
"active": "a4aa933fc978113e",
"active": "28a54cc81af4607b",
"lastOpenFiles": [
"Coding Tips (Classical)/Terminal Tips/GUIs/Games/gdevelop.io.md",
"C++.md",
"Coding Tips (Classical)/Terminal Tips/Languages/C++.md",
"Coding Tips (Classical)/Terminal Tips/GUIs/Games/Neopets.md",
"Coding Tips (Classical)/Terminal Tips/GUIs/Games/Ruffle.md",
"Coding Tips (Classical)/Terminal Tips/GUIs/Tools/StackBlitz.md",

View File

@ -1,6 +0,0 @@
# C++
C++ is one of the most used languages out there for writing low-level code. There are various applications and projects based off of C++.
- A URL shortener in C++
- gist r

View File

@ -0,0 +1,112 @@
# C++
C++ is one of the most used languages out there for writing low-level code. There are various applications and projects based off of C++.
- A URL shortener in C++
- [gist](https://gist.github.com/jaytaylor/a11fadf61a869ade0dfe568606b216c8#file-base64-url-shortener-poc-cpp)
```c++
/**
* URL shortener Base-62 encoder / decoder C++ Proof-of-Concept.
*
* Created as a resource for https://stackoverflow.com/a/742047/293064.
*
* How to compile:
*
* g++ -o base64-url-shortener-poc base64-url-shortener-poc.cpp
*
* Usage:
*
* ./base64-url-shortener-poc [ID]
*
* Example output:
*
* Input ID: 99592
* ---
* Generated short URL: z4u
* ID decoded from URL: 99592
*
* @author Jay Taylor <outtatime@gmail.com>
*
* @date 2019-07-08
*
*/
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
/**
* id_to_short_url takes an input ID and produces a short URL suffix.
*
* Base-62 encodes the identifier.
*/
string id_to_short_url(unsigned int n) {
// Mapping which defines the 62 possible output characters.
char map[] = "abcdefghijklmnopqrstuvwxyzABCDEF"
"GHIJKLMNOPQRSTUVWXYZ0123456789";
string short_url;
// Convert given ID to a base-62 number.
while (n) {
// Append each character mapped by the remainder.
short_url.push_back(map[n % 62]);
n /= 62;
}
// Reverse the string to complete the base conversion.
reverse(short_url.begin(), short_url.end());
return short_url;
}
/**
* short_url_to_id converts a short URL into the corresponding ID.
*
* Base-62 decodes the input string.
*/
unsigned int short_url_to_id(string short_url) {
unsigned int id = 0;
// Base decode conversion logic.
for (int i = 0; i < short_url.length(); ++i) {
if ('a' <= short_url[i] && short_url[i] <= 'z') {
id = id * 62 + short_url[i] - 'a';
}
if ('A' <= short_url[i] && short_url[i] <= 'Z') {
id = id * 62 + short_url[i] - 'A' + 26;
}
if ('0' <= short_url[i] && short_url[i] <= '9') {
id = id * 62 + short_url[i] - '0' + 52;
}
}
return id;
}
int main(int argc, char** argv) {
if (argc == 1) {
cerr << "error: missing required parameter: id" << endl;
return 1;
}
unsigned int n = atoi(argv[1]);
if (n <= 0) {
cerr << "error: invalid input value, an integer greater than 0 is required" << endl;
return 1;
}
cout << "Input ID: " << n << endl << "---" << endl;
string encoded_short_url = id_to_short_url(n);
unsigned int decoded_id = short_url_to_id(encoded_short_url);
cout << "Generated short URL: " << encoded_short_url << endl;
cout << "ID decoded from URL: " << decoded_id << endl;
return 0;
}
```