Passerby_A
BAN USERstd::string to_dollar(const int amt) {
std::deque<std::string> csRslt;
int rVal = amt;
while (rVal != 0) {
int cVal = rVal % 1000;
rVal /= 1000;
std::stringstream ss;
ss << cVal;
csRslt.push_front(ss.str());
}
std::string csAmnt("$");
for (size_t i = 0; i < csRslt.size(); ++i) {
csAmnt += csRslt[i];
if (i != (csRslt.size()-1)) csAmnt += ",";
}
return csAmnt;
}
a zombie process has completed execution; a orphan process is still executing.
- Passerby_A March 16, 2014std::make_heap in C++
- Passerby_A March 16, 2014input is not integer.
- Passerby_A March 16, 2014in C++: the operator new allocate memory only; the new operator allocate memory, call constructor, and return pointer to the allocated address.
- Passerby_A March 15, 2014How about the case [1, 3, 5, 2, 6, 8, 9].
- Passerby_A March 15, 2014can we use condition variables in simulating the join (in C++)?
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
std::mutex mtx;
std::condition_variable cv;
int counter = 0;
void print_dummy(const int id) {
std::lock_guard<std::mutex> lck(mtx);
std::cout << "---" << id << "+++" << std::endl;
std::this_thread::sleep_for(std::chrono::microseconds(100));
counter++;
cv.notify_one();
}
int main() {
const int threads_counter = 10;
std::vector<std::thread> tLst;
for (int i = 0; i < threads_counter; ++i) {
tLst.push_back(std::thread(print_dummy, i));
}
std::unique_lock<std::mutex> lck(mtx);
while (counter != threads_counter) cv.wait(lck);
std::cout << "Press enter to continue ..." << std::endl;
std::cin.get();
for (auto& t : tLst) t.detach();
return 0;
}
using C++11.
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
const int max = 100;
std::mutex mtx;
std::condition_variable cv;
bool bEven = true, bOdd = false;
void countAndPrint(const int strVal, const int id) {
int n = strVal;
while (n < max) {
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [&]() { return n % 2 == 0 ? bEven : bOdd; });
std::cout << "thread: " << id << ", " << n << std::endl;
n += 2;
bEven = !bEven;
bOdd = !bOdd;
cv.notify_one();
}
}
int main() {
std::thread t1(countAndPrint, 0, 0);
std::thread t2(countAndPrint, 1, 1);
t1.join();
t2.join();
std::cin.get();
return 0;
}
use conditional variable to make the threads run sequentially.
- Passerby_A March 07, 2014#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
int x = 0;
std::once_flag flg;
void setX(const int n) {
x = n;
std::cout << "setX: " << n << std::endl;
}
void wait_and_set(const int id) {
std::this_thread::sleep_for(std::chrono::microseconds(100));
std::call_once(flg, setX, id);
}
int main() {
std::vector<std::thread> tLst;
for (int i = 1; i < 10; ++i) {
tLst.push_back(std::thread(wait_and_set, i));
}
for (auto& t : tLst) t.join();
std::cout << "The final value of x is " << x << std::endl;
std::cin.get();
std::cout << "Press Enter to continue ..." << std::endl;
return 0;
}
How about the following, there is no lock used.
#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
std::vector<int> rslt(5, 0);
void myadd(const int s, const int e, const int id) {
int r = 0;
for (int i = s; i <= e; ++i) {
r += i;
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
rslt[id] = r;
}
int main() {
std::thread t1(myadd, 1, 5, 0);
std::thread t2(myadd, 1, 50, 1);
std::thread t3(myadd, 5, 15, 2);
std::thread t4(myadd, 10, 20, 3);
std::thread t5(myadd, 15, 20, 4);
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
for (size_t i = 0; i < rslt.size(); ++i) std::cout << rslt[i] << std::endl;
std::cout << "Press enter to continue ..." << std::endl;
std::cin.get();
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string str1("abcdefg"), str2("defghig");
vector<vector<int> > arr;
int maxLen = 0;
string comStr;
for (int i = 0; i < str1.size(); ++i) {
vector<int> row(str2.size(), 0);
arr.push_back(row);
for (int j = 0; j < str2.size(); ++j) {
if (str1[i] == str2[j]) {
if (i == 0 || j == 0) {
arr[i][j] = 1;
}
else {
arr[i][j] = arr[i - 1][j - 1] + 1;
}
if (arr[i][j] > maxLen) {
maxLen = arr[i][j];
comStr = str1.substr(i - maxLen + 1, maxLen);
}
}
else {
arr[i][j] = 0;
}
}
}
cout << "common string: " << comStr << ", with max lengh of " << maxLen << endl;
cin.get();
return 0;
}
CTest.hpp
#include <iostream>
class CTest {
private:
int m_nVal;
static CTest * m_pInstance;
//! Constructor.
CTest() : m_nVal(0) {
std::cout << "CTest(): " << m_nVal << std::endl;
}
//! Disable copy constructor.
CTest(const CTest&) = delete;
//! Disable assignment operator.
CTest& operator = (const CTest&) = delete;
public:
//!Distructor
~CTest() {
std::cout << "~CTest()" << std::endl;
}
static CTest& getInstance();
};
CTest.cpp
#include "CTest.hpp"
#include <mutex>
std::once_flag o_flg;
CTest* CTest::m_pInstance = NULL;
CTest& CTest::getInstance() {
call_once(o_flg, []() {
if (!m_pInstance) {
m_pInstance = new CTest();
}
}
);
return *m_pInstance;
}
main.cpp
#include "CTest.hpp"
#include <thread>
#include <vector>
void createInst() {
CTest& mInst = CTest::getInstance();
}
int main() {
{
std::vector<std::thread> tLst;
for (size_t i = 0; i < 100; i++) {
tLst.push_back(std::move(std::thread(createInst)));
}
for (auto& t : tLst) t.join();
}
std::cin.get();
return 0;
}
#include <iostream>
#include <vector>
#include <set>
int main() {
std::vector<int> first = { 5, 4, 3, 2, 1, 6, 7, 8};
std::vector<int> second = { 5, 6, 7, 8, 9, 10, 11, 20, 19, 18};
std::set<int> rSet(&first[0], &first[0] + first.size());
rSet.insert(second.begin(), second.end());
std::vector<int> third(rSet.begin(), rSet.end());
for (size_t i = 0; i < third.size(); ++i) {
std::cout << third[i] << std::endl;
}
std::cin.get();
return 0;
}
O(nlogn).
- Passerby_A March 05, 2014#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex mtx;
std::condition_variable cv;
bool doPrintOdd = false;
bool doPrintEven= true;
int nMax = 100;
void printOdd() {
for (int i = 0; i < nMax; ++i) {
std::unique_lock<std::mutex> lck(mtx);
while (!doPrintOdd) cv.wait(lck);
std::cout << 2 * i + 1 << std::endl;
doPrintOdd = false;
doPrintEven = true;
cv.notify_one();
}
}
void printEven() {
for (int i = 0; i < nMax; ++i) {
std::unique_lock<std::mutex> lck(mtx);
while (!doPrintEven) cv.wait(lck);
std::cout << 2 * i << std::endl;
doPrintEven = false;
doPrintOdd = true;
cv.notify_one();
}
}
int main() {
std::thread t1(printOdd);
std::thread t2(printEven);
t1.join();
t2.join();
std::cout << "Press Enter to continue ..." << std::endl;
std::cin.get();
return 0;
}
#include <iostream>
using namespace std;
class A {
public:
static A* getInstance() { return new A; }
~A() { cout << "~A()" << endl; }
private:
A() { cout << "A()" << endl; }
};
int main() {
//A a;
A * ptrA = A::getInstance();
delete ptrA;
getchar();
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arr[] = { 1, 3, 4, 10, 18, 3, 2, 6, 3, 1, 4 };
int nLen = sizeof(arr) / sizeof(int);
int i = 0, j = nLen - 1;
int sum1 = 0, sum2 = 0;
while (i < j) {
if (sum1 >= sum2) {
sum2 += arr[j--];
}
else {
sum1 += arr[i++];
}
}
getchar();
return 0;
}
how about a diamond with edge of equal length.
- Passerby_A February 22, 2014#include <iostream>
#include <stdio.h>
using namespace std;
void doReverse(char *pszStr, int sIdx, int eIdx) {
while (sIdx < eIdx) {
char cTmp = pszStr[sIdx];
pszStr[sIdx] = pszStr[eIdx];
pszStr[eIdx] = cTmp;
sIdx++; eIdx--;
}
}
void doReverseOfSentence(char * csMsg) {
int nLen = strlen(csMsg);
doReverse(csMsg, 0, nLen - 1);
int i = 0, sIdx = 0;
while (i <= nLen) {
if (csMsg[i] == ' ' || csMsg[i] == '\0') {
doReverse(csMsg, sIdx, i - 1);
sIdx = i + 1;
}
i++;
}
}
int main() {
char csMsg[] = "Today is Saturday";
cout << "original string: " << csMsg << endl;
doReverseOfSentence(csMsg);
cout << "afterwarth string: " << csMsg << endl;
getchar();
return 0;
}
- Passerby_A March 21, 2014