snipt

Ctrl+h for KB shortcuts

C++

map: get value by key

1
2
3
4
5
6
7
map<int,Bar>::iterator it = m.find('2');
Bar b3;
if(it != m.end())
{
   //element found;
   b3 = it->second;
}
https://snippets.siftie.com/embed/d44388a5089c6f6f4ef13ed0824992e3/
/raw/d44388a5089c6f6f4ef13ed0824992e3/
d44388a5089c6f6f4ef13ed0824992e3
cpp
C++
7
2019-03-20T08:36:59
True
False
False
Aug 20, 2012 at 02:55 AM
/api/public/snipt/45976/
map-get-value-by-key
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">map</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="n">Bar</span><span class="o">&gt;::</span><span class="n">iterator</span> <span class="n">it</span> <span class="o">=</span> <span class="n">m</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="sc">&#39;2&#39;</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="n">Bar</span> <span class="n">b3</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="k">if</span><span class="p">(</span><span class="n">it</span> <span class="o">!=</span> <span class="n">m</span><span class="p">.</span><span class="n">end</span><span class="p">())</span> </span><span id="L-4"><a name="L-4"></a><span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="c1">//element found;</span> </span><span id="L-6"><a name="L-6"></a> <span class="n">b3</span> <span class="o">=</span> <span class="n">it</span><span class="o">-&gt;</span><span class="n">second</span><span class="p">;</span> </span><span id="L-7"><a name="L-7"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
C++, map

Text only

去除char array后的换行符

int len = strlen(a);
a[len-1] = 0;
https://snippets.siftie.com/embed/90652f9ee650fc319b09ac97c2418e24/
/raw/90652f9ee650fc319b09ac97c2418e24/
90652f9ee650fc319b09ac97c2418e24
text
Text only
2
2019-03-05T16:12:50
True
False
False
/api/public/snipt/43403/
char-array
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>int len = strlen(a); </span><span id="L-2"><a name="L-2"></a>a[len-1] = 0; </span></pre></div> </td></tr></table>
string

C#

Get Embedded Resource

Stream fileStream= Assembly.GetExecutingAssembly().GetManifestResourceStream("NameSpace.FileNameWithExtension");
https://snippets.siftie.com/embed/c09658f584e5bcea0f623c463bbc3dc2/
/raw/c09658f584e5bcea0f623c463bbc3dc2/
c09658f584e5bcea0f623c463bbc3dc2
csharp
C#
1
2019-03-23T06:26:57
True
False
False
/api/public/snipt/41223/
get-embedded-resource
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">Stream</span> <span class="n">fileStream</span><span class="p">=</span> <span class="n">Assembly</span><span class="p">.</span><span class="n">GetExecutingAssembly</span><span class="p">().</span><span class="n">GetManifestResourceStream</span><span class="p">(</span><span class="s">&quot;NameSpace.FileNameWithExtension&quot;</span><span class="p">);</span> </span></pre></div> </td></tr></table>
reflection

C#

Ignore Cross-thread InvalidOperationException when Debugging

Control.CheckForIllegalCrossThreadCalls = false;
https://snippets.siftie.com/embed/fba3e1dde9bfdf41b17ca0bc914dbdb2/
/raw/fba3e1dde9bfdf41b17ca0bc914dbdb2/
fba3e1dde9bfdf41b17ca0bc914dbdb2
csharp
C#
1
2019-03-19T21:57:19
True
False
False
/api/public/snipt/38782/
ignore-cross-thread-invalidoperationexception-when-debugging
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">Control</span><span class="p">.</span><span class="n">CheckForIllegalCrossThreadCalls</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span> </span></pre></div> </td></tr></table>
BackgroundWorker, Thread

C#

Change the time of a DateTime

1
2
3
DateTime s = ...;
TimeSpan ts = new TimeSpan(10, 30, 0);
s = s.Date + ts;
https://snippets.siftie.com/embed/24b8fd009a48adaa0050a06412cad859/
/raw/24b8fd009a48adaa0050a06412cad859/
24b8fd009a48adaa0050a06412cad859
csharp
C#
3
2019-03-04T15:07:19
True
False
False
/api/public/snipt/38773/
change-the-time-of-a-datetime
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">DateTime</span> <span class="n">s</span> <span class="p">=</span> <span class="p">...;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">TimeSpan</span> <span class="n">ts</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TimeSpan</span><span class="p">(</span><span class="m">10</span><span class="p">,</span> <span class="m">30</span><span class="p">,</span> <span class="m">0</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="n">s</span> <span class="p">=</span> <span class="n">s</span><span class="p">.</span><span class="n">Date</span> <span class="p">+</span> <span class="n">ts</span><span class="p">;</span> </span></pre></div> </td></tr></table>
datetime