StackOverflow.com

VN:F [1.9.22_1171]
Rating: 8.5/10 (13 votes cast)

Random snippets of all sorts of code, mixed with a selection of help and advice.

C++ vector push_back not working correctly inside loop

3 May 2026 @ 11:34 am

Title: C++ vector push_back not working correctly inside loop Body: I am trying to insert elements into a std::vector using push_back() inside a loop, but the vector is not storing values as expected. Problem I expect the vector to store all values added inside the loop, but it either stores incorrect values or appears empty when accessed later. Code #include <iostream> #include <vector> using namespace std; int main() { vector<int> v; for(int i = 0; i < 5; i++) { v.push_back(i); } for(int i = 0; i < v.size(); i++) { cout << v[i] << " "; } return 0; } Expected Output 0 1 2 3 4 Actual Output (Output is not as expected or vector appears empty)

React 19 use() hook with Suspense boundaries: unexpected re-renders on cached promises

3 May 2026 @ 11:28 am

I've been migrating a mid-sized app to React 19 and started using the new use() hook to read promises directly in components. The issue I'm running into is that even with a stable promise reference (memoized with useMemo), the component inside the Suspense boundary seems to re-render more than expected when a parent state changes. Here's a minimal reproduction: function DataComponent({ promise }) { const data = use(promise); console.log("rendered"); // fires more than expected return <div>{data.name}</div>; } function Parent() { const [count, setCount] = useState(0); const promise = useMemo(() => fetchUser(1), []); // stable reference return ( <> <button onClick={() => setCount(c => c + 1)}>increment {count}</button> <Suspense fallback={<p>Loading...</p>}> <DataComponent promise={promise} /> </Suspense> </&g

KDTree 2d index searching based on pivot_table vector error

3 May 2026 @ 11:19 am

i want find 2d index(from PCA). based on pivot_table vector at my custom metric. fastly. that's why im using KDTree. but because it, these error is occur: ValueError Traceback (most recent call last) /tmp/ipykernel_9428/753399497.py in <cell line: 0>() 50 return 10.0 51 ---> 52 matriks_jarak = metrics(Matrix.values, Matrix.values, 5) 53 54 model = knn(n_neighbors=6, metric="precomputed") /tmp/ipykernel_9428/753399497.py in metrics(x, x2, n_tetangga) 31 def metrics(x, x2, n_tetangga): 32 global ind, jarak, ind0_nya, rigth_arr, sudah_print ---> 33 ind_x = pohon.query(x.reshape(1, -1), k=1, return_distance=False)[0][0] 34 ind_x2 = pohon.query(x2.reshape(1, -1), k=1, return_distance=False)[0][0] 35 jarak_final = jarak.tolist()[0] sklearn/neighbors/_binary_tree.pxi in sklearn.neighbors._kd_tree.BinaryTree64.query() ValueError: query data dimension must match trai

setPointerCapture prevents pointerup/mouseup from firing in JUCE's WKWebView (macOS)

3 May 2026 @ 11:14 am

I'm building a drag-to-reorder interaction for horizontal card layout inside a JUCE plugin's WebBrowserComponent (backed by WKWebView on macOS). The card moves correctly during drag but the release event (pointerup, mouseup) never fires. What works: Knob drag: canvas.setPointerCapture(id) + canvas.addEventListener('pointermove', ...) + canvas.addEventListener('pointerup', ...) → fully functional Slider drag: same pattern on a <div> → fully functional What doesn't work: Node card horizontal drag: identical pattern on a card <div> inside an overflow-x: auto scroll container → pointermove fires (card translates), but pointerup and mouseup never fire on either the element OR document

C, structs and pointers - why does this work?

3 May 2026 @ 11:09 am

I wanted to try using pointers within structs to create dynamic arrays (don't know if that makes sense, I'm relatively new to C.) I started using calloc() to allocate memory, but I noticed that the program would still work even without using calloc(). I can't understand why this works. Is this something you can do? #include <stdlib.h> #include <stdio.h> struct foo { int *age; }; struct bar { int *year; }; struct comb { struct foo f; struct bar b; }; int main() { struct comb c; c.f.age[0] = 23; c.f.age[1] = 67; c.f.age[2] = 14; c.f.age[3] = 1; c.f.age[4] = 3; c.b.year[0] = 1972; c.b.year[1] = 1982; c.b.year[2] = 1988; c.b.year[3] = 1997; c.b.year[4] = 2002; int i = 0; while (i < 5) { printf("%d %d\n", c.f.age[i], c.b.year[i]); i++; } return 0; } Output:

How to Programmatically audit page score by category in TypeScript

3 May 2026 @ 11:09 am

I'm building a headless CMS where I want to automatically audit each page's SEO before it goes live. I need a solution that: Returns a 0–100 score broken down by category (meta tags, content, structure, performance) Flags issues with severity levels (error, warning, info) so I can prioritize fixes Works server-side in Node.js without making any external API calls Can block a CI/CD deployment if the score drops below a threshold What I've tried: I've been manually checking things like title length and meta description, but it's tedious and doesn't scale: // Manual checks — inconsistent and hard to maintain const titleLength = title.length; if (titleLength < 50 || titleLength > 60) { console.warn('Title length issue'); } const descLength = metaDescription.length; if (descLength <

VS Code Branch Switching (post worktree vscode update)

3 May 2026 @ 10:04 am

TLDR: Vscode Git: Checkout to... command used to allow selecting branches. Now it forces you to Choose a repository and only then the branch. Does anyone know how to make it offer Select a branch or tag to checkout as it used to? Or even better turn off the introduced worktree support altogether? Clicking directly on branch name (bottom left corner) still works as intended, but I cannot find a way of binding this exact functionality to a shortcut. Either I don´t understand how to use it or vscode worktree support is a complete mess. I had been using worktrees for years but since the support was introduced I had to constantly fight it to prevent clutter and noice it introduces to the UI. I started by rolling back vscode version util extension updates forced me to update my vscode version. Then I deactivated Git: Detect Worktrees and set Git: Detect Worktrees Limit to 0, this helped for so

Python re.compile and REGEX patterns to clean-up name variations

3 May 2026 @ 7:52 am

I am looking for help with a REGEX pattern that will help me clean-up names to just the first, last, and suffix. It needs to be able to handle the following variations. Drops any middle name Keeps last name if any Drops any nicknames Drops any maiden or dead names, standard format (name-) Keeps suffixes without punctuation. Always follows ,\s after last name. Does not keep titles of nobility. Always follows ,\s after last name. Sarah Michelle Gellar -> Sarah Gellar Leslie Marie DeGuzman Santorini -> Leslie Santorini Beyonce -> Beyonce Stefani "Lady Gaga" Germanotta -> Stefani Germanotta Lebron James, Jr. -> Lebron James Jr Hailey Bieber (Baldwin-) -> Hailey Bieber Shania (Irene-) Twain -> Shania Twain Charles "Chaz" Fluffy Wong, Duke of CatTown -> Charles Wong I atte

should i remove the return?

3 May 2026 @ 7:45 am

useEffect(() => { const handleScroll = () =>{ if(window.scrollY > 100) { setShowModal(true); window.removeEventListener("scroll", handleScroll); } }; window.addEventListener("scroll", handleScroll); return () => window.removeEventListener("scroll", handleScroll) }, []) should i remove this code? return () => window.removeEventListener("scroll", handleScroll) since at my ifStatement has already? i guess no need to return right? window.removeEventListener("scroll", handleScroll);

Pooling model estimates with MICE from competing risks crr survival models gives 1 degree of freedom and wide CI

3 May 2026 @ 6:45 am

I am imputing cause of death in a data of a clinical cohort. The imputation process is working without problems. However, when I try to estimate hazards ratio from using fine-grey model, I get one degree of freedom, which results in confidence intervals in the infinity !! This problem isn't appearing when I run a coxph model instead, which means the problem probably likes in how the crr models are pooled together. Here is an example using the trial data frame from tidycmprsk package to reproduce the problem. x <- tidycmprsk::trial set.seed(123) # Set seed for reproducibility # 1. Randomly select the indices to be turned into NA na_indices <- sample(which(x$death_cr %in% c("death from cancer", "death other causes")), 24) # 2. Create the new variable x$death_cr_new <- x$death_cr x$death_cr_new[na_indices] <- NA # 3. Verification check print(table(x$death_cr, useNA = "always")) print(tab