'Best way to implement fixed size array in C++
I am trying to implement a fixed size array of 32 bit integers, but the size is determined at runtime so it has to be heap allocated. Would it be more efficient to use a c++ std::vector and use vector.reserve() or should I use the conventional way of doing it using new int32_t[size]? Additionally, would it be worth using a unique_ptr with the conventional method using new?
Solution 1:[1]
- to plot a scatter (which you referred to as bubble) you need the GPS co-ordinates of cities. Have sourced these from here: https://simplemaps.com/data/world-cities
- you have not provided sample data of a dataframe which contains list of cities. Have generated a random list
delist - now it's a simple case of
- aggregate cities to number of times in list
dflist.groupby("city", as_index=False).size() - merge geometry
merge(dfuk, on="city") - generate plotly figure
- aggregate cities to number of times in list
import sys, requests, urllib
import pandas as pd
from pathlib import Path
from zipfile import ZipFile
import plotly.express as px
# fmt: off
# download data set, world cities including GPS co-ordinates
url = "https://simplemaps.com/static/data/world-cities/basic/simplemaps_worldcities_basicv1.74.zip"
f = Path.cwd().joinpath(f'{urllib.parse.urlparse(url).path.split("/")[-1]}.zip')
if not f.exists():
r = requests.get(url, stream=True, headers={"User-Agent": "XY"})
with open(f, "wb") as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)
zfile = ZipFile(f)
dfs = {f.filename: pd.read_csv(zfile.open(f)) for f in zfile.infolist() if f.filename.split(".")[1]=="csv"}
# fmt: on
# just UK cities.. with GPS
dfuk = dfs["worldcities.csv"].loc[lambda d: d["iso3"].eq("GBR")]
# dataframe referred to in question, cities listed multiple times
n_cities = 20
p = np.geomspace(0.01, 1, n_cities)
p = p / p.sum()
dflist = pd.DataFrame({"city": np.random.choice(dfuk.sample(n_cities)["city"], 500, p=p)})
px.scatter_mapbox(
dflist.groupby("city", as_index=False).size().merge(dfuk, on="city"),
lat="lat",
lon="lng",
hover_name="city",
size="size",
).update_layout(mapbox={"style": "carto-positron", "zoom": 4}, margin={"t":0,"b":0,"l":0,"r":0})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rob Raymond |

